hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cf_queue.h
Go to the documentation of this file.
1 /*
2  * Citrusleaf Foundation
3  * include/queue.h - queue structures
4  *
5  * Copyright 2008 by Citrusleaf. All rights reserved.
6  * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE. THE COPYRIGHT NOTICE
7  * ABOVE DOES NOT EVIDENCE ANY ACTUAL OR INTENDED PUBLICATION.
8  */
9 
10 #pragma once
11 
12 #include <pthread.h>
13 #include <stddef.h>
14 #include <stdint.h>
15 
17 
18 
19 /* SYNOPSIS
20  * Queue
21  */
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 
28 /* cf_queue
29  * A queue */
30 #define CF_QUEUE_ALLOCSZ 64
31 typedef struct cf_queue_s {
32  bool threadsafe; // sometimes it's good to live dangerously
33  unsigned int allocsz; // number of queue elements currently allocated
34  unsigned int write_offset; // 0 offset is first queue element.
35  // write is always greater than or equal to read
36  unsigned int read_offset; //
37  size_t elementsz; // number of bytes in an element
38 #ifdef EXTERNAL_LOCKS
39  void *LOCK; // the lock object
40 #else
41  pthread_mutex_t LOCK; // the mutex lock
42  pthread_cond_t CV; // hte condvar
43 #endif // EXTERNAL_LOCKS
44  uint8_t *queue; // the actual bytes that make up the queue
45 } cf_queue;
46 
47 #define CF_Q_SZ(__q) (__q->write_offset - __q->read_offset)
48 
49 #define CF_Q_EMPTY(__q) (__q->write_offset == __q->read_offset)
50 
51 // todo: maybe it's faster to keep the read and write offsets in bytes,
52 // to avoid the extra multiply?
53 #define CF_Q_ELEM_PTR(__q, __i) (&__q->queue[ (__i % __q->allocsz) * __q->elementsz ] )
54 
55 
56 /* External functions */
57 extern cf_queue *cf_queue_create(size_t elementsz, bool threadsafe);
58 
59 extern void cf_queue_destroy(cf_queue *q);
60 
61 // Always pushes to the end of the queue
62 extern int cf_queue_push(cf_queue *q, void *ptr);
63 
64 // Push element on the queue only if size < limit.
65 extern bool cf_queue_push_limit(cf_queue *q, void *ptr, uint32_t limit);
66 
67 // Get the number of elements currently in the queue
68 extern int cf_queue_sz(cf_queue *q);
69 
70 
71 
72 
73 // POP pops from the end of the queue, which is the most efficient
74 // But understand this makes it LIFO, the least fair of queues
75 // Elements added at the very beginning might not make it out
76 
77 #define CF_QUEUE_EMPTY -2
78 #define CF_QUEUE_ERR -1
79 #define CF_QUEUE_OK 0
80 
81 // mswait < 0 wait forever
82 // mswait == 0 wait not at all
83 // mswait > 0 wait that number of ms
84 #define CF_QUEUE_FOREVER -1
85 #define CF_QUEUE_NOWAIT 0
86 extern int cf_queue_pop(cf_queue *q, void *buf, int mswait);
87 
88 // Queue Reduce
89 // Run the entire queue, calling the callback, with the lock held
90 // You can return values in the callback to cause deletes
91 // Great for purging dying stuff out of a queue synchronously
92 //
93 // Return -2 from the callback to trigger a delete
94 // return -1 stop iterating the queue
95 // return 0 for success
96 typedef int (*cf_queue_reduce_fn) (void *buf, void *udata);
97 
98 extern int cf_queue_reduce(cf_queue *q, cf_queue_reduce_fn cb, void *udata);
99 
100 //
101 // The most common reason to want to 'reduce' is delete - so provide
102 // a simple delete function
103 extern int cf_queue_delete(cf_queue *q, void *buf, bool only_one);
104 
105 
106 //
107 // A simple priority queue implementation, which is simply a set of queues
108 // underneath
109 // This currently doesn't support 'delete' and 'reduce' functionality
110 //
111 
112 typedef struct cf_queue_priority_s {
117 #ifdef EXTERNAL_LOCKS
118  void * LOCK;
119 #else
120  pthread_mutex_t LOCK;
121  pthread_cond_t CV;
122 #endif
124 
125 #define CF_QUEUE_PRIORITY_HIGH 1
126 #define CF_QUEUE_PRIORITY_MEDIUM 2
127 #define CF_QUEUE_PRIORITY_LOW 3
128 
129 #define CF_Q_PRI_EMPTY(__q) (CF_Q_EMPTY(__q->low_q) && CF_Q_EMPTY(__q->medium_q) && CF_Q_EMPTY(__q->high_q))
130 
131 extern cf_queue_priority *cf_queue_priority_create(size_t elementsz, bool threadsafe);
133 extern int cf_queue_priority_push(cf_queue_priority *q, void *ptr, int pri);
134 extern int cf_queue_priority_pop(cf_queue_priority *q, void *buf, int mswait);
136 
137 
138 
139 //
140 // Call this function to do a set of internal validation unit tests
141 // 0 means success, and it blocks until complete
142 
143 extern int cf_queue_test();
144 
145 #ifdef __cplusplus
146 } // end extern "C"
147 #endif
148