1 |
/* Creation date: 2005-07-26 08:19:33
|
2 |
* Authors: Don
|
3 |
* Change log:
|
4 |
*/
|
5 |
|
6 |
|
7 |
/* Copyright (c) 2005 Don Owens
|
8 |
All rights reserved.
|
9 |
|
10 |
This code is released under the BSD license:
|
11 |
|
12 |
Redistribution and use in source and binary forms, with or without
|
13 |
modification, are permitted provided that the following conditions
|
14 |
are met:
|
15 |
|
16 |
* Redistributions of source code must retain the above copyright
|
17 |
notice, this list of conditions and the following disclaimer.
|
18 |
|
19 |
* Redistributions in binary form must reproduce the above
|
20 |
copyright notice, this list of conditions and the following
|
21 |
disclaimer in the documentation and/or other materials provided
|
22 |
with the distribution.
|
23 |
|
24 |
* Neither the name of the author nor the names of its
|
25 |
contributors may be used to endorse or promote products derived
|
26 |
from this software without specific prior written permission.
|
27 |
|
28 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
29 |
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
30 |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
31 |
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
32 |
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
33 |
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
34 |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
35 |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
36 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
37 |
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
38 |
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
39 |
OF THE POSSIBILITY OF SUCH DAMAGE.
|
40 |
*/
|
41 |
|
42 |
#ifndef _CFUTHREAD_QUEUE_H
|
43 |
#define _CFUTHREAD_QUEUE_H
|
44 |
|
45 |
#include <cfu.h>
|
46 |
|
47 |
#ifdef __cplusplus
|
48 |
extern "C" {
|
49 |
#endif
|
50 |
|
51 |
/* cfuthread_queue provides a way to serialize requests for a
|
52 |
* resource where you want the resource to be accessed from a
|
53 |
* single thread only. For instance, for a database connection
|
54 |
* where making calls in separate threads does not work properly,
|
55 |
* you can use cfuthread_queue. cfuthread_queue_new() creates a
|
56 |
* new thread that waits for something to be added to the queue.
|
57 |
* Once something is added, the thread will process the data by
|
58 |
* calling the function you pass as an argument to the
|
59 |
* cfuthread_queue_new() function.
|
60 |
*/
|
61 |
|
62 |
struct cfuthread_queue;
|
63 |
typedef struct cfuthread_queue cfuthread_queue_t;
|
64 |
|
65 |
typedef void * (*cfuthread_queue_fn_t)(void *arg);
|
66 |
typedef void (*cfuthread_queue_init_t)(void *arg);
|
67 |
typedef void (*cfuthread_queue_cleanup_t)(void *arg);
|
68 |
|
69 |
/* Creates a new thread queue structure that will run the given
|
70 |
* function when a request is received.
|
71 |
*/
|
72 |
extern cfuthread_queue_t * cfuthread_queue_new(cfuthread_queue_fn_t fn);
|
73 |
|
74 |
/* Same as cfuthread_queue_new(), but with an initialization
|
75 |
* function that gets called with the argument init_arg when the
|
76 |
* thread is created, and a cleanup function that gets called with
|
77 |
* the argument cleanup_arg when the thread exits, e.g., when
|
78 |
* cfuthread_queue_destroy() is called.
|
79 |
*/
|
80 |
extern cfuthread_queue_t * cfuthread_queue_new_with_cleanup(cfuthread_queue_fn_t fn,
|
81 |
cfuthread_queue_init_t init_fn, void *init_arg, cfuthread_queue_cleanup_t cleanup_fn,
|
82 |
void *cleanup_arg);
|
83 |
|
84 |
/* Add a request to the queue. data will get passed to the
|
85 |
* function fn given to cfuthread_queue_new when it reaches the
|
86 |
* front of the queue.
|
87 |
*/
|
88 |
extern void * cfuthread_queue_make_request(cfuthread_queue_t * tq, void *data);
|
89 |
|
90 |
/* Free up resources used by the queue, in addition to canceling
|
91 |
* the thread.
|
92 |
*/
|
93 |
extern void cfuthread_queue_destroy(cfuthread_queue_t *);
|
94 |
|
95 |
#ifdef __cplusplus
|
96 |
}
|
97 |
#endif
|
98 |
|
99 |
|
100 |
#endif
|