From ec688b4723a041044226358bcd4dd6e2da39da49 Mon Sep 17 00:00:00 2001 From: Luca Muscariello Date: Thu, 23 Feb 2017 17:01:02 +0100 Subject: Initial commit: cframework. Longbow and Libparc Change-Id: I90378dbd30da6033b20fb1f829b3b822cf366c59 Signed-off-by: Luca Muscariello --- libparc/parc/concurrent/parc_Synchronizer.c | 146 ++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100755 libparc/parc/concurrent/parc_Synchronizer.c (limited to 'libparc/parc/concurrent/parc_Synchronizer.c') diff --git a/libparc/parc/concurrent/parc_Synchronizer.c b/libparc/parc/concurrent/parc_Synchronizer.c new file mode 100755 index 00000000..c4475476 --- /dev/null +++ b/libparc/parc/concurrent/parc_Synchronizer.c @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2017 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + */ +#include + +#include +#include + +#include + +#ifdef PARCLibrary_DISABLE_ATOMICS +# include +#else +//# include +#endif + +typedef enum { + _PARCSynchronizer_Unlocked = 0, + _PARCSynchronizer_Locked = 1 +} _PARCSynchronizer; + +struct PARCSynchronizer { +#ifdef PARCLibrary_DISABLE_ATOMICS + pthread_mutex_t mutex; +#else + int mutex; +#endif +}; + +static void +_parcSynchronizer_Finalize(PARCSynchronizer **instancePtr) +{ + assertNotNull(instancePtr, "Parameter must be a non-null pointer to a PARCSynchronizer pointer."); + + parcSynchronizer_OptionalAssertValid((*instancePtr)); +} + +parcObject_ImplementAcquire(parcSynchronizer, PARCSynchronizer); + +parcObject_ImplementRelease(parcSynchronizer, PARCSynchronizer); + +parcObject_ExtendPARCObject(PARCSynchronizer, _parcSynchronizer_Finalize, NULL, NULL, NULL, NULL, NULL, NULL); + +void +parcSynchronizer_AssertValid(const PARCSynchronizer *instance) +{ + assertTrue(parcSynchronizer_IsValid(instance), + "PARCSynchronizer is not valid."); +} + +PARCSynchronizer * +parcSynchronizer_Create(void) +{ + PARCSynchronizer *result = parcObject_CreateInstance(PARCSynchronizer); + +#ifdef PARCLibrary_DISABLE_ATOMICS + pthread_mutex_init(&result->mutex, NULL); +#else + result->mutex = _PARCSynchronizer_Unlocked; +#endif + + return result; +} + +void +parcSynchronizer_Display(const PARCSynchronizer *instance, int indentation) +{ + parcDisplayIndented_PrintLine(indentation, "PARCSynchronizer@%p {", instance); + /* Call Display() functions for the fields here. */ + parcDisplayIndented_PrintLine(indentation, "}"); +} + +bool +parcSynchronizer_IsValid(const PARCSynchronizer *instance) +{ + bool result = false; + + if (instance != NULL) { + result = true; + } + + return result; +} + +bool +parcSynchronizer_TryLock(PARCSynchronizer *instance) +{ +#ifdef PARCLibrary_DISABLE_ATOMICS + bool result = pthread_mutex_trylock(&instance->mutex) == 0; +#else + bool result = __sync_bool_compare_and_swap(&instance->mutex, _PARCSynchronizer_Unlocked, _PARCSynchronizer_Locked); +#endif + return result; +} + +void +parcSynchronizer_Lock(PARCSynchronizer *instance) +{ +#ifdef PARCLibrary_DISABLE_ATOMICS + pthread_mutex_lock(&instance->mutex); +#else + while (!__sync_bool_compare_and_swap(&instance->mutex, _PARCSynchronizer_Unlocked, _PARCSynchronizer_Locked)) { + ; + } +#endif +} + +void +parcSynchronizer_Unlock(PARCSynchronizer *instance) +{ +#ifdef PARCLibrary_DISABLE_ATOMICS + pthread_mutex_unlock(&instance->mutex); +#else + while (!__sync_bool_compare_and_swap(&instance->mutex, _PARCSynchronizer_Locked, _PARCSynchronizer_Unlocked)) { + ; + } +#endif +} + +bool +parcSynchronizer_IsLocked(const PARCSynchronizer *barrier) +{ +#ifdef PARCLibrary_DISABLE_ATOMICS + PARCSynchronizer *instance = (PARCSynchronizer *) barrier; + + bool result = pthread_mutex_trylock(&instance->mutex) != 0; + pthread_mutex_unlock(&instance->mutex); + return result; +#else + return barrier->mutex == _PARCSynchronizer_Locked; +#endif +} -- cgit 1.2.3-korg