diff options
author | Marti Maria <info@littlecms.com> | 2014-02-14 17:00:53 +0100 |
---|---|---|
committer | Marti Maria <info@littlecms.com> | 2014-02-14 17:00:53 +0100 |
commit | 82aede053180359fb353a223a88ec56d6f3c52c1 (patch) | |
tree | b45688e4dcefe5ba4bf7bc3b34645782ef0dc9c0 /src | |
parent | 65da8ef0925c442b88d5a4f01cd8feecf0f809b2 (diff) | |
download | lcms2-82aede053180359fb353a223a88ec56d6f3c52c1.tar.gz |
First attempt to pthread integration
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.in | 1 | ||||
-rw-r--r-- | src/cmserr.c | 119 | ||||
-rw-r--r-- | src/cmsio0.c | 5 |
3 files changed, 121 insertions, 4 deletions
diff --git a/src/Makefile.in b/src/Makefile.in index 158fc01..69887c2 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -175,6 +175,7 @@ LIBTOOL = @LIBTOOL@ LIBTOOL_DEPS = @LIBTOOL_DEPS@ LIB_JPEG = @LIB_JPEG@ LIB_MATH = @LIB_MATH@ +LIB_PTHREAD = @LIB_PTHREAD@ LIB_TIFF = @LIB_TIFF@ LIB_ZLIB = @LIB_ZLIB@ LIPO = @LIPO@ diff --git a/src/cmserr.c b/src/cmserr.c index 4bfc0a7..ba0a07e 100644 --- a/src/cmserr.c +++ b/src/cmserr.c @@ -532,14 +532,125 @@ void _cmsTagSignature2String(char String[5], cmsTagSignature sig) //-------------------------------------------------------------------------------------------------- +#ifndef CMS_NO_PTHREADS + +#ifdef CMS_IS_WINDOWS_ + +// Windows funtions +#define WIN32_LEAN_AND_MEAN 1 +#include <Windows.h> + +static +void* defMtxCreate(cmsContext id) +{ + cmsUNUSED_PARAMETER(id); + return (void*) CreateMutex( NULL, FALSE, NULL); +} + +static +void defMtxDestroy(cmsContext id, void* mtx) +{ + cmsUNUSED_PARAMETER(id); + CloseHandle((HANDLE) mtx); +} + +static +cmsBool defMtxLock(cmsContext id, void* mtx) +{ + cmsUNUSED_PARAMETER(id); + WaitForSingleObject((HANDLE) mtx, INFINITE); + return TRUE; +} + +static +void defMtxUnlock(cmsContext id, void* mtx) +{ + cmsUNUSED_PARAMETER(id); + ReleaseMutex((HANDLE) mtx); +} + +#else + +// Rest of the wide world +#include <pthread.h> + +static +void* defMtxCreate(cmsContext id) +{ + pthread_mutex_t* ptr_mutex = _cmsMalloc(id, sizeof(pthread_mutex_t)); + + *ptr_mutex = PTHREAD_MUTEX_INITIALIZER; + if (pthread_mutex_init(ptr_mutex, NULL) != 0) { + return NULL; + } + + return (void*) ptr_mutex; +} + +static +void defMtxDestroy(cmsContext id, void* mtx) +{ + pthread_mutex_destroy((pthread_mutex_t *) mtx); + _cmsFree(id, mtx); +} + +static +cmsBool defMtxLock(cmsContext id, void* mtx) +{ + cmsUNUSED_PARAMETER(id); + return pthread_mutex_lock((pthread_mutex_t *) mtx) == 0; +} + +static +void defMtxUnlock(cmsContext id, void* mtx) +{ + cmsUNUSED_PARAMETER(id); + pthread_mutex_unlock((pthread_mutex_t *) mtx); +} + +#endif +#else + +// Multithreading locking is disabled +static +void* defMtxCreate(cmsContext id) +{ + return NULL; +} + +static +void defMtxDestroy(cmsContext id, void* mtx) +{ + cmsUNUSED_PARAMETER(id); + cmsUNUSED_PARAMETER(mtx); +} + +static +cmsBool defMtxLock(cmsContext id, void* mtx) +{ + cmsUNUSED_PARAMETER(id); + cmsUNUSED_PARAMETER(mtx); + + return TRUE; +} + +static +void defMtxUnlock(cmsContext id, void* mtx) +{ + cmsUNUSED_PARAMETER(id); + cmsUNUSED_PARAMETER(mtx); +} + +#endif + // Pointers to memory manager functions in Context0 -_cmsMutexPluginChunkType _cmsMutexPluginChunk = { NULL, NULL, NULL, NULL }; +_cmsMutexPluginChunkType _cmsMutexPluginChunk = { defMtxCreate, defMtxDestroy, defMtxLock, defMtxUnlock }; // Allocate and init mutex container. void _cmsAllocMutexPluginChunk(struct _cmsContext_struct* ctx, const struct _cmsContext_struct* src) { - static _cmsMutexPluginChunkType MutexChunk = { NULL, NULL, NULL, NULL }; + static _cmsMutexPluginChunkType MutexChunk = {defMtxCreate, defMtxDestroy, defMtxLock, defMtxUnlock }; void* from; if (src != NULL) { @@ -573,9 +684,9 @@ cmsBool _cmsRegisterMutexPlugin(cmsContext ContextID, cmsPluginBase* Data) Plugin ->LockMutexPtr == NULL || Plugin ->UnlockMutexPtr == NULL) return FALSE; - ctx->CreateMutexPtr = Plugin->CreateMutexPtr; + ctx->CreateMutexPtr = Plugin->CreateMutexPtr; ctx->DestroyMutexPtr = Plugin ->DestroyMutexPtr; - ctx ->LockMutexPtr = Plugin ->LockMutexPtr; + ctx ->LockMutexPtr = Plugin ->LockMutexPtr; ctx ->UnlockMutexPtr = Plugin ->UnlockMutexPtr; // All is ok diff --git a/src/cmsio0.c b/src/cmsio0.c index 0bb156f..4449acd 100644 --- a/src/cmsio0.c +++ b/src/cmsio0.c @@ -1709,6 +1709,7 @@ cmsInt32Number CMSEXPORT cmsReadRawTag(cmsHPROFILE hProfile, cmsTagSignature sig if (!Icc ->IOhandler ->Seek(Icc ->IOhandler, Offset)) goto Error; if (!Icc ->IOhandler ->Read(Icc ->IOhandler, data, 1, TagSize)) goto Error; + _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex); return TagSize; } @@ -1738,7 +1739,11 @@ cmsInt32Number CMSEXPORT cmsReadRawTag(cmsHPROFILE hProfile, cmsTagSignature sig // Already readed, or previously set by cmsWriteTag(). We need to serialize that // data to raw in order to maintain consistency. + + _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex); Object = cmsReadTag(hProfile, sig); + if (!_cmsLockMutex(Icc->ContextID, Icc ->UsrMutex)) return 0; + if (Object == NULL) goto Error; // Now we need to serialize to a memory block: just use a memory iohandler |