Blender  V2.59
BLI_threads.h
Go to the documentation of this file.
00001 /*
00002  *
00003  * $Id: BLI_threads.h 34966 2011-02-18 13:58:08Z jesterking $
00004  *
00005  * ***** BEGIN GPL LICENSE BLOCK *****
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software Foundation,
00019  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  * The Original Code is Copyright (C) 2006 Blender Foundation.
00022  * All rights reserved.
00023  *
00024  * The Original Code is: all of this file.
00025  *
00026  * Contributor(s): none yet.
00027  *
00028  * ***** END GPL LICENSE BLOCK *****
00029  */
00030 
00031 #ifndef BLI_THREADS_H
00032 #define BLI_THREADS_H 
00033 
00038 #include <pthread.h>
00039 
00040 /* for tables, button in UI, etc */
00041 #define BLENDER_MAX_THREADS             64
00042 
00043 struct ListBase;
00044 
00045 /* Threading API */
00046 
00047 /*this is run once at startup*/
00048 void BLI_threadapi_init(void);
00049 
00050 void    BLI_init_threads        (struct ListBase *threadbase, void *(*do_thread)(void *), int tot);
00051 int             BLI_available_threads(struct ListBase *threadbase);
00052 int             BLI_available_thread_index(struct ListBase *threadbase);
00053 void    BLI_insert_thread       (struct ListBase *threadbase, void *callerdata);
00054 void    BLI_remove_thread       (struct ListBase *threadbase, void *callerdata);
00055 void    BLI_remove_thread_index(struct ListBase *threadbase, int index);
00056 void    BLI_remove_threads(struct ListBase *threadbase);
00057 void    BLI_end_threads         (struct ListBase *threadbase);
00058 int BLI_thread_is_main(void);
00059 
00060 /* System Information */
00061 
00062 int             BLI_system_thread_count(void); /* gets the number of threads the system can make use of */
00063 
00064 /* Global Mutex Locks
00065  * 
00066  * One custom lock available now. can be extended. */
00067 
00068 #define LOCK_IMAGE              0
00069 #define LOCK_PREVIEW    1
00070 #define LOCK_VIEWER             2
00071 #define LOCK_CUSTOM1    3
00072 #define LOCK_RCACHE             4
00073 #define LOCK_OPENGL             5
00074 
00075 void    BLI_lock_thread(int type);
00076 void    BLI_unlock_thread(int type);
00077 
00078 /* Mutex Lock */
00079 
00080 typedef pthread_mutex_t ThreadMutex;
00081 #define BLI_MUTEX_INITIALIZER   PTHREAD_MUTEX_INITIALIZER
00082 
00083 void BLI_mutex_init(ThreadMutex *mutex);
00084 void BLI_mutex_lock(ThreadMutex *mutex);
00085 void BLI_mutex_unlock(ThreadMutex *mutex);
00086 void BLI_mutex_end(ThreadMutex *mutex);
00087 
00088 /* Read/Write Mutex Lock */
00089 
00090 #define THREAD_LOCK_READ        1
00091 #define THREAD_LOCK_WRITE       2
00092 
00093 typedef pthread_rwlock_t ThreadRWMutex;
00094 
00095 void BLI_rw_mutex_init(ThreadRWMutex *mutex);
00096 void BLI_rw_mutex_lock(ThreadRWMutex *mutex, int mode);
00097 void BLI_rw_mutex_unlock(ThreadRWMutex *mutex);
00098 void BLI_rw_mutex_end(ThreadRWMutex *mutex);
00099 
00100 /* ThreadedWorker
00101  *
00102  * A simple tool for dispatching work to a limited number of threads
00103  * in a transparent fashion from the caller's perspective. */
00104 
00105 struct ThreadedWorker;
00106 
00107 /* Create a new worker supporting tot parallel threads.
00108  * When new work in inserted and all threads are busy, sleep(sleep_time) before checking again
00109  */
00110 struct ThreadedWorker *BLI_create_worker(void *(*do_thread)(void *), int tot, int sleep_time);
00111 
00112 /* join all working threads */
00113 void BLI_end_worker(struct ThreadedWorker *worker);
00114 
00115 /* also ends all working threads */
00116 void BLI_destroy_worker(struct ThreadedWorker *worker);
00117 
00118 /* Spawns a new work thread if possible, sleeps until one is available otherwise
00119  * NOTE: inserting work is NOT thread safe, so make sure it is only done from one thread */
00120 void BLI_insert_work(struct ThreadedWorker *worker, void *param);
00121 
00122 /* ThreadWorkQueue
00123  *
00124  * Thread-safe work queue to push work/pointers between threads. */
00125 
00126 typedef struct ThreadQueue ThreadQueue;
00127 
00128 ThreadQueue *BLI_thread_queue_init(void);
00129 void BLI_thread_queue_free(ThreadQueue *queue);
00130 
00131 void BLI_thread_queue_push(ThreadQueue *queue, void *work);
00132 void *BLI_thread_queue_pop(ThreadQueue *queue);
00133 void *BLI_thread_queue_pop_timeout(ThreadQueue *queue, int ms);
00134 int BLI_thread_queue_size(ThreadQueue *queue);
00135 
00136 void BLI_thread_queue_nowait(ThreadQueue *queue);
00137 
00138 #endif
00139