📄 gthread.c
字号:
gpointer ddata = node->data; GDestroyNotify ddestroy = node->destroy; node->data = data; node->destroy = notify; ddestroy (ddata); } else { node->data = data; node->destroy = notify; }}void g_static_private_free (GStaticPrivate *private_key){ guint index = private_key->index; GSList *list; if (!index) return; private_key->index = 0; G_LOCK (g_thread); list = g_thread_all_threads; while (list) { GRealThread *thread = list->data; GArray *array = thread->private_data; list = list->next; if (array && index <= array->len) { GStaticPrivateNode *node = &g_array_index (array, GStaticPrivateNode, index - 1); gpointer ddata = node->data; GDestroyNotify ddestroy = node->destroy; node->data = NULL; node->destroy = NULL; if (ddestroy) { G_UNLOCK (g_thread); ddestroy (ddata); G_LOCK (g_thread); } } } g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces, GUINT_TO_POINTER (index)); G_UNLOCK (g_thread);}static voidg_thread_cleanup (gpointer data){ if (data) { GRealThread* thread = data; if (thread->private_data) { GArray* array = thread->private_data; guint i; for (i = 0; i < array->len; i++ ) { GStaticPrivateNode *node = &g_array_index (array, GStaticPrivateNode, i); if (node->destroy) node->destroy (node->data); } g_array_free (array, TRUE); } /* We only free the thread structure, if it isn't joinable. If it is, the structure is freed in g_thread_join */ if (!thread->thread.joinable) { G_LOCK (g_thread); g_thread_all_threads = g_slist_remove (g_thread_all_threads, data); G_UNLOCK (g_thread); /* Just to make sure, this isn't used any more */ g_system_thread_assign (thread->system_thread, zero_thread); g_free (thread); } }}static voidg_thread_fail (void){ g_error ("The thread system is not yet initialized.");}static gpointerg_thread_create_proxy (gpointer data){ GRealThread* thread = data; g_assert (data);#ifdef G_THREAD_USE_PID_SURROGATE thread->pid = getpid ();#endif /* G_THREAD_USE_PID_SURROGATE */ /* This has to happen before G_LOCK, as that might call g_thread_self */ g_private_set (g_thread_specific_private, data); /* the lock makes sure, that thread->system_thread is written, before thread->thread.func is called. See g_thread_create. */ G_LOCK (g_thread); G_UNLOCK (g_thread); #ifdef G_THREAD_USE_PID_SURROGATE if (g_thread_use_default_impl) SET_PRIO (thread->pid, thread->thread.priority);#endif /* G_THREAD_USE_PID_SURROGATE */ thread->retval = thread->thread.func (thread->thread.data); return NULL;}GThread* g_thread_create_full (GThreadFunc func, gpointer data, gulong stack_size, gboolean joinable, gboolean bound, GThreadPriority priority, GError **error){ GRealThread* result; GError *local_error = NULL; g_return_val_if_fail (func, NULL); g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL); g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL); result = g_new (GRealThread, 1); result->thread.joinable = joinable; result->thread.priority = priority; result->thread.func = func; result->thread.data = data; result->private_data = NULL; G_LOCK (g_thread); G_THREAD_UF (thread_create, (g_thread_create_proxy, result, stack_size, joinable, bound, priority, &result->system_thread, &local_error)); g_thread_all_threads = g_slist_prepend (g_thread_all_threads, result); G_UNLOCK (g_thread); if (local_error) { g_propagate_error (error, local_error); g_free (result); return NULL; } return (GThread*) result;}voidg_thread_exit (gpointer retval){ GRealThread* real = (GRealThread*) g_thread_self (); real->retval = retval; G_THREAD_CF (thread_exit, (void)0, ());}gpointerg_thread_join (GThread* thread){ GRealThread* real = (GRealThread*) thread; gpointer retval; g_return_val_if_fail (thread, NULL); g_return_val_if_fail (thread->joinable, NULL); g_return_val_if_fail (!g_system_thread_equal (real->system_thread, zero_thread), NULL); G_THREAD_UF (thread_join, (&real->system_thread)); retval = real->retval; G_LOCK (g_thread); g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread); G_UNLOCK (g_thread); /* Just to make sure, this isn't used any more */ thread->joinable = 0; g_system_thread_assign (real->system_thread, zero_thread); /* the thread structure for non-joinable threads is freed upon thread end. We free the memory here. This will leave a loose end, if a joinable thread is not joined. */ g_free (thread); return retval;}voidg_thread_set_priority (GThread* thread, GThreadPriority priority){ GRealThread* real = (GRealThread*) thread; g_return_if_fail (thread); g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread)); g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW); g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT); thread->priority = priority;#ifdef G_THREAD_USE_PID_SURROGATE if (g_thread_use_default_impl) SET_PRIO (real->pid, priority); else#endif /* G_THREAD_USE_PID_SURROGATE */ G_THREAD_CF (thread_set_priority, (void)0, (&real->system_thread, priority));}GThread*g_thread_self (void){ GRealThread* thread = g_private_get (g_thread_specific_private); if (!thread) { /* If no thread data is available, provide and set one. This can happen for the main thread and for threads, that are not created by GLib. */ thread = g_new (GRealThread, 1); thread->thread.joinable = FALSE; /* This is a save guess */ thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is just a guess */ thread->thread.func = NULL; thread->thread.data = NULL; thread->private_data = NULL; if (g_thread_supported ()) G_THREAD_UF (thread_self, (&thread->system_thread));#ifdef G_THREAD_USE_PID_SURROGATE thread->pid = getpid ();#endif /* G_THREAD_USE_PID_SURROGATE */ g_private_set (g_thread_specific_private, thread); G_LOCK (g_thread); g_thread_all_threads = g_slist_prepend (g_thread_all_threads, thread); G_UNLOCK (g_thread); } return (GThread*)thread;}voidg_static_rw_lock_init (GStaticRWLock* lock){ static GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT; g_return_if_fail (lock); *lock = init_lock;}static void inline g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex){ if (!*cond) *cond = g_cond_new (); g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));}static void inline g_static_rw_lock_signal (GStaticRWLock* lock){ if (lock->want_to_write && lock->write_cond) g_cond_signal (lock->write_cond); else if (lock->want_to_read && lock->read_cond) g_cond_broadcast (lock->read_cond);}void g_static_rw_lock_reader_lock (GStaticRWLock* lock){ g_return_if_fail (lock); if (!g_threads_got_initialized) return; g_static_mutex_lock (&lock->mutex); lock->want_to_read++; while (lock->write || lock->want_to_write) g_static_rw_lock_wait (&lock->read_cond, &lock->mutex); lock->want_to_read--; lock->read_counter++; g_static_mutex_unlock (&lock->mutex);}gboolean g_static_rw_lock_reader_trylock (GStaticRWLock* lock){ gboolean ret_val = FALSE; g_return_val_if_fail (lock, FALSE); if (!g_threads_got_initialized) return TRUE; g_static_mutex_lock (&lock->mutex); if (!lock->write && !lock->want_to_write) { lock->read_counter++; ret_val = TRUE; } g_static_mutex_unlock (&lock->mutex); return ret_val;}void g_static_rw_lock_reader_unlock (GStaticRWLock* lock){ g_return_if_fail (lock); if (!g_threads_got_initialized) return; g_static_mutex_lock (&lock->mutex); lock->read_counter--; if (lock->read_counter == 0) g_static_rw_lock_signal (lock); g_static_mutex_unlock (&lock->mutex);}void g_static_rw_lock_writer_lock (GStaticRWLock* lock){ g_return_if_fail (lock); if (!g_threads_got_initialized) return; g_static_mutex_lock (&lock->mutex); lock->want_to_write++; while (lock->write || lock->read_counter) g_static_rw_lock_wait (&lock->write_cond, &lock->mutex); lock->want_to_write--; lock->write = TRUE; g_static_mutex_unlock (&lock->mutex);}gboolean g_static_rw_lock_writer_trylock (GStaticRWLock* lock){ gboolean ret_val = FALSE; g_return_val_if_fail (lock, FALSE); if (!g_threads_got_initialized) return TRUE; g_static_mutex_lock (&lock->mutex); if (!lock->write && !lock->read_counter) { lock->write = TRUE; ret_val = TRUE; } g_static_mutex_unlock (&lock->mutex); return ret_val;}void g_static_rw_lock_writer_unlock (GStaticRWLock* lock){ g_return_if_fail (lock); if (!g_threads_got_initialized) return; g_static_mutex_lock (&lock->mutex); lock->write = FALSE; g_static_rw_lock_signal (lock); g_static_mutex_unlock (&lock->mutex);}void g_static_rw_lock_free (GStaticRWLock* lock){ g_return_if_fail (lock); if (lock->read_cond) { g_cond_free (lock->read_cond); lock->read_cond = NULL; } if (lock->write_cond) { g_cond_free (lock->write_cond); lock->write_cond = NULL; } g_static_mutex_free (&lock->mutex);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -