diff options
author | Vsevolod Stakhov <vsevolod@rambler-co.ru> | 2012-04-13 19:46:05 +0400 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rambler-co.ru> | 2012-04-13 19:46:05 +0400 |
commit | 637d09abaceb0e3babff9fd25fa133da4c0732ae (patch) | |
tree | 5b66351754f9666a43cc681cd896e268a361bf34 /src/util.h | |
parent | efe165bc3d0f8b225be40bb8bd0bfebf7e972f04 (diff) | |
download | rspamd-637d09abaceb0e3babff9fd25fa133da4c0732ae.tar.gz rspamd-637d09abaceb0e3babff9fd25fa133da4c0732ae.zip |
Add portable api for locks and threads to handle Glib threads API change.
Diffstat (limited to 'src/util.h')
-rw-r--r-- | src/util.h | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/util.h b/src/util.h index e520c9d2d..8db8cc878 100644 --- a/src/util.h +++ b/src/util.h @@ -227,4 +227,82 @@ gint rspamd_fallocate (gint fd, off_t offset, off_t len); */ worker_t* get_worker_by_type (GQuark type); +/** + * Utils for working with threads to be compatible with all glib versions + */ +typedef struct rspamd_mutex_s { +#if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION > 30)) + GMutex mtx; +#else + GStaticMutex mtx; +#endif +} rspamd_mutex_t; + +typedef struct rspamd_rwlock_s { +#if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION > 30)) + GRWLock rwlock; +#else + GStaticRWLock rwlock; +#endif +} rspamd_rwlock_t; + + +/** + * Create new mutex + * @return mutex or NULL + */ +rspamd_mutex_t* rspamd_mutex_new (); + +/** + * Lock mutex + * @param mtx + */ +void rspamd_mutex_lock (rspamd_mutex_t *mtx); + +/** + * Unlock mutex + * @param mtx + */ +void rspamd_mutex_unlock (rspamd_mutex_t *mtx); + +/** + * Create new rwloc + * @return + */ +rspamd_rwlock_t* rspamd_rwlock_new (); + +/** + * Lock rwlock for writing + * @param mtx + */ +void rspamd_rwlock_writer_lock (rspamd_rwlock_t *mtx); + +/** + * Lock rwlock for reading + * @param mtx + */ +void rspamd_rwlock_reader_lock (rspamd_rwlock_t *mtx); + +/** + * Unlock rwlock from writing + * @param mtx + */ +void rspamd_rwlock_writer_unlock (rspamd_rwlock_t *mtx); + +/** + * Unlock rwlock from reading + * @param mtx + */ +void rspamd_rwlock_reader_unlock (rspamd_rwlock_t *mtx); + +/** + * Create new named thread + * @param name name pattern + * @param func function to start + * @param data data to pass to function + * @param err error pointer + * @return new thread object that can be joined + */ +GThread* rspamd_create_thread (const gchar *name, GThreadFunc func, gpointer data, GError **err); + #endif |