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