1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
/**************************************************************************\
*
* This file is part of the Coin 3D visualization library.
* Copyright (C) by Kongsberg Oil & Gas Technologies.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* ("GPL") version 2 as published by the Free Software Foundation.
* See the file LICENSE.GPL at the root directory of this source
* distribution for additional information about the GNU GPL.
*
* For using Coin with software that can not be combined with the GNU
* GPL, and for taking advantage of the additional benefits of our
* support services, please contact Kongsberg Oil & Gas Technologies
* about acquiring a Coin Professional Edition License.
*
* See http://www.coin3d.org/ for more information.
*
* Kongsberg Oil & Gas Technologies, Bygdoy Alle 5, 0257 Oslo, NORWAY.
* http://www.sim.no/ sales@sim.no coin-support@coin3d.org
*
\**************************************************************************/
#include <Inventor/C/threads/sync.h>
#include <stddef.h>
#include <Inventor/C/threads/mutex.h>
#include "tidbitsp.h"
#include "base/dict.h"
#include "threads/syncp.h"
#include "threads/mutexp.h"
/* ********************************************************************** */
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
static cc_dict * sync_hash_table = NULL;
static void
sync_hash_cb(uintptr_t key, void * val, void * closure)
{
cc_mutex_destruct((cc_mutex*) val);
}
static void
sync_cleanup(void)
{
cc_dict_apply(sync_hash_table, sync_hash_cb, NULL);
cc_dict_destruct(sync_hash_table);
sync_hash_table = NULL;
}
/*
Initialize synchronizer. Should only be called once, from SoDB::init()
*/
void
cc_sync_init(void)
{
if (sync_hash_table == NULL) {
/* the priority is set so to make this callback trigger late,
after normal cleanup function which might still use a cc_sync
instance */
coin_atexit((coin_atexit_f*) sync_cleanup, CC_ATEXIT_THREADING_SUBSYSTEM);
sync_hash_table = cc_dict_construct(256, 0.75f);
}
}
/*!
Begin synchronizing \a id. After returning from this call, other
threads trying to synchronize \a id will be blocked until the first
thread calls cc_sync_end() with the key returned from this function.
*/
void *
cc_sync_begin(void * id)
{
void * mutex;
cc_mutex_global_lock();
if (sync_hash_table == NULL) {
cc_sync_init();
}
if (!cc_dict_get(sync_hash_table, (uintptr_t)id, &mutex)) {
mutex = (void*) cc_mutex_construct();
(void) cc_dict_put(sync_hash_table, (uintptr_t)id, mutex);
}
cc_mutex_global_unlock();
cc_mutex_lock((cc_mutex*) mutex);
return mutex;
}
/*!
End synchronize. \a key should be the key returned from cc_sync_begin().
*/
void
cc_sync_end(void * key)
{
cc_mutex_unlock((cc_mutex*) key);
}
/*!
Free sync item. This can be called from the destructor of objects
that use a sync item so that the sync-mutex is freed when the object
is destructed.
\since Coin 2.3
*/
void
cc_sync_free(void * id)
{
void * mutex;
cc_mutex_global_lock();
if (sync_hash_table == NULL) {
cc_sync_init();
}
if (cc_dict_get(sync_hash_table, (uintptr_t)id, &mutex)) {
cc_mutex_destruct((cc_mutex*) mutex);
cc_dict_remove(sync_hash_table, (uintptr_t)id);
}
cc_mutex_global_unlock();
}
#ifdef __cplusplus
} /* extern "C" */
#endif /* __cplusplus */
|