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