blob: 3e8031e897b84da4073cd769a839b940e72c60d0 [file] [log] [blame]
Florin Corase86a8ed2018-01-05 03:20:25 -08001/*
2 *------------------------------------------------------------------
3 * svm_queue.h - shared-memory queues
4 *
5 * Copyright (c) 2009 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
18 */
19
20#ifndef included_svm_queue_h
21#define included_svm_queue_h
22
23#include <pthread.h>
24
25typedef struct _svm_queue
26{
27 pthread_mutex_t mutex; /* 8 bytes */
28 pthread_cond_t condvar; /* 8 bytes */
29 int head;
30 int tail;
31 volatile int cursize;
32 int maxsize;
33 int elsize;
34 int consumer_pid;
Florin Corasc470e222018-08-01 07:53:18 -070035 int producer_evtfd;
36 int consumer_evtfd;
Florin Corase86a8ed2018-01-05 03:20:25 -080037 char data[0];
38} svm_queue_t;
39
Mohsin Kazmi3fca5672018-01-04 18:57:26 +010040typedef enum
41{
Florin Corasc470e222018-08-01 07:53:18 -070042 SVM_Q_WAIT = 0, /**< blocking call - must be used only in combination
43 with condvars */
44 SVM_Q_NOWAIT, /**< non-blocking call - works with both condvar and
45 eventfd signaling */
46 SVM_Q_TIMEDWAIT, /**< blocking call, returns on signal or time-out -
47 must be used only in combination with condvars */
Mohsin Kazmi3fca5672018-01-04 18:57:26 +010048} svm_q_conditional_wait_t;
49
Florin Corasc470e222018-08-01 07:53:18 -070050/**
51 * Allocate and initialize svm queue
52 *
53 * @param nels number of elements on the queue
54 * @param elsize element size, presumably 4 and cacheline-size will
55 * be popular choices.
56 * @param pid consumer pid
57 * @return a newly initialized svm queue
58 *
59 * The idea is to call this function in the queue consumer,
60 * and e-mail the queue pointer to the producer(s).
61 *
62 * The vpp process / main thread allocates one of these
63 * at startup; its main input queue. The vpp main input queue
64 * has a pointer to it in the shared memory segment header.
65 *
66 * You probably want to be on an svm data heap before calling this
67 * function.
68 */
69svm_queue_t *svm_queue_alloc_and_init (int nels, int elsize,
70 int consumer_pid);
71svm_queue_t *svm_queue_init (void *base, int nels, int elsize);
Florin Corase86a8ed2018-01-05 03:20:25 -080072void svm_queue_free (svm_queue_t * q);
73int svm_queue_add (svm_queue_t * q, u8 * elem, int nowait);
74int svm_queue_add2 (svm_queue_t * q, u8 * elem, u8 * elem2, int nowait);
Mohsin Kazmi3fca5672018-01-04 18:57:26 +010075int svm_queue_sub (svm_queue_t * q, u8 * elem, svm_q_conditional_wait_t cond,
76 u32 time);
Florin Corase86a8ed2018-01-05 03:20:25 -080077int svm_queue_sub2 (svm_queue_t * q, u8 * elem);
78void svm_queue_lock (svm_queue_t * q);
79void svm_queue_unlock (svm_queue_t * q);
80int svm_queue_is_full (svm_queue_t * q);
81int svm_queue_add_nolock (svm_queue_t * q, u8 * elem);
82int svm_queue_sub_raw (svm_queue_t * q, u8 * elem);
Florin Coras3c2fed52018-07-04 04:15:05 -070083
84/**
Florin Coras99368312018-08-02 10:45:44 -070085 * Wait for queue event
86 *
87 * Must be called with mutex held.
88 */
89void svm_queue_wait (svm_queue_t * q);
90
91/**
92 * Timed wait for queue event
93 *
94 * Must be called with mutex held.
95 *
96 * @param q svm queue
97 * @param timeout time in seconds
98 * @return 0 on success, ETIMEDOUT on timeout or an error
99 */
100int svm_queue_timedwait (svm_queue_t * q, double timeout);
101
102/**
Florin Coras3c2fed52018-07-04 04:15:05 -0700103 * Add element to queue with mutex held
104 * @param q queue
105 * @param elem pointer element data to add
106 */
107void svm_queue_add_raw (svm_queue_t * q, u8 * elem);
Florin Corase86a8ed2018-01-05 03:20:25 -0800108
Florin Corasc470e222018-08-01 07:53:18 -0700109/**
110 * Set producer's event fd
111 *
112 * When the producer must generate an event it writes 1 to the provided fd.
113 * Once this is set, condvars are not used anymore for signaling.
114 */
115void svm_queue_set_producer_event_fd (svm_queue_t * q, int fd);
116
117/**
118 * Set consumer's event fd
119 *
120 * When the consumer must generate an event it writes 1 to the provided fd.
121 * Although in practice the two fds point to the same underlying file
122 * description, because the producer and consumer are different processes
123 * the descriptors will be different. It's the caller's responsibility to
124 * ensure the file descriptors are properly exchanged between the two peers.
125 */
126void svm_queue_set_consumer_event_fd (svm_queue_t * q, int fd);
127
Florin Corase86a8ed2018-01-05 03:20:25 -0800128/*
129 * DEPRECATED please use svm_queue_t instead
130 */
131typedef svm_queue_t unix_shared_memory_queue_t;
132
133#endif /* included_svm_queue_h */
134
135/*
136 * fd.io coding-style-patch-verification: ON
137 *
138 * Local Variables:
139 * eval: (c-set-style "gnu")
140 * End:
141 */