1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3 * Copyright (c) 2008 Carnegie Mellon University. All rights
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * This work was supported in part by funding from the Defense Advanced
19 * Research Projects Agency and the National Science Foundation of the
20 * United States of America, and the CMU Sphinx Speech Consortium.
22 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
23 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
26 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * ====================================================================
39 * @brief Simple portable thread functions.
40 * @author David Huggins-Daines <dhuggins@cs.cmu.edu>
43 #ifndef __SBTHREAD_H__
44 #define __SBTHREAD_H__
46 #include <sphinx_config.h>
48 #include <sphinxbase/sphinxbase_export.h>
49 #include <sphinxbase/cmd_ln.h>
62 typedef struct sbthread_s sbthread_t;
65 * Asynchronous message queue object.
67 typedef struct sbmsgq_s sbmsgq_t;
70 * Mutex (critical section) object.
72 typedef struct sbmtx_s sbmtx_t;
77 typedef struct sbevent_s sbevent_t;
80 * Entry point for a thread.
82 typedef int (*sbthread_main)(sbthread_t *th);
88 sbthread_t *sbthread_start(cmd_ln_t *config, sbthread_main func, void *arg);
91 * Wait for a thread to complete.
94 int sbthread_wait(sbthread_t *th);
97 * Free a thread object.
100 void sbthread_free(sbthread_t *th);
103 * Get configuration object from a thread.
106 cmd_ln_t *sbthread_config(sbthread_t *th);
109 * Get argument pointer from a thread.
112 void *sbthread_arg(sbthread_t *th);
115 * Get message queue from a thread.
118 sbmsgq_t *sbthread_msgq(sbthread_t *th);
121 * Wait for a thread to complete.
124 int sbthread_wait(sbthread_t *th);
127 * Send an asynchronous message to a thread.
129 * Each thread gets a message queue by default, so this is just a
130 * wrapper around sbmsgq_send().
133 int sbthread_send(sbthread_t *th, size_t len, void const *data);
136 * Create a message queue.
138 * @param depth Depth of the queue.
141 sbmsgq_t *sbmsgq_init(size_t depth);
144 * Free a message queue.
147 void sbmsgq_free(sbmsgq_t *q);
150 * Post a message to a queue.
153 int sbmsgq_send(sbmsgq_t *q, size_t len, void const *data);
156 * Wait for a message from a queue.
159 void *sbmsgq_wait(sbmsgq_t *q, size_t *out_len, int sec, int nsec);
165 sbmtx_t *sbmtx_init(void);
168 * Try to acquire a mutex.
171 int sbmtx_trylock(sbmtx_t *mtx);
177 int sbmtx_lock(sbmtx_t *mtx);
183 int sbmtx_unlock(sbmtx_t *mtx);
186 * Dispose of a mutex.
189 void sbmtx_free(sbmtx_t *mtx);
192 * Initialize an event.
195 sbevent_t *sbevent_init(void);
201 void sbevent_free(sbevent_t *evt);
207 int sbevent_signal(sbevent_t *evt);
210 * Wait for an event to be signalled.
213 int sbevent_wait(sbevent_t *evt, int sec, int nsec);
221 #endif /* __SBTHREAD_H__ */