Skip to content

Commit

Permalink
Change call_once to pthread_once
Browse files Browse the repository at this point in the history
  • Loading branch information
zxjtan committed Oct 5, 2023
1 parent bac0b23 commit 8aa10f5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
6 changes: 6 additions & 0 deletions src/common/sha3/xkcp_dispatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
#ifndef OQS_SHA3_XKCP_DISPATCH_H
#define OQS_SHA3_XKCP_DISPATCH_H

#if defined __has_include
#if __has_include (<pthread.h>)
#define OQS_SHA3_XKCP_DISPATCH_USE_PTHREAD
#endif
#endif

typedef void KeccakInitFn(void *);
extern KeccakInitFn \
KeccakP1600_Initialize, \
Expand Down
14 changes: 7 additions & 7 deletions src/common/sha3/xkcp_sha3.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@

#include <oqs/common.h>

#ifdef OQS_SHA3_XKCP_DISPATCH_USE_PTHREAD
#include <pthread.h>
#endif
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#ifndef __STDC_NO_THREADS__
#include <threads.h>
#endif

#define KECCAK_CTX_ALIGNMENT 32
#define _KECCAK_CTX_BYTES (200+sizeof(uint64_t))
#define KECCAK_CTX_BYTES (KECCAK_CTX_ALIGNMENT * \
((_KECCAK_CTX_BYTES + KECCAK_CTX_ALIGNMENT - 1)/KECCAK_CTX_ALIGNMENT))

#ifndef __STDC_NO_THREADS__
static once_flag dispatch_once_flag = ONCE_FLAG_INIT;
#ifdef OQS_SHA3_XKCP_DISPATCH_USE_PTHREAD
static pthread_once_t dispatch_once_control = PTHREAD_ONCE_INIT;
#endif

static KeccakInitFn *Keccak_Initialize_ptr = NULL;
Expand Down Expand Up @@ -85,8 +85,8 @@ static void Keccak_Dispatch() {
* that have not been permuted, or not-yet-squeezed bytes.
**************************************************/
static void keccak_inc_reset(uint64_t *s) {
#ifndef __STDC_NO_THREADS__
call_once(&dispatch_once_flag, Keccak_Dispatch);
#ifdef OQS_SHA3_XKCP_DISPATCH_USE_PTHREAD
pthread_once(&dispatch_once_control, Keccak_Dispatch);
#else
if (Keccak_Initialize_ptr == NULL) {
Keccak_Dispatch();
Expand Down
14 changes: 7 additions & 7 deletions src/common/sha3/xkcp_sha3x4.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@
#include <oqs/common.h>
#include <oqs/oqsconfig.h>

#ifdef OQS_SHA3_XKCP_DISPATCH_USE_PTHREAD
#include <pthread.h>
#endif
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#ifndef __STDC_NO_THREADS__
#include <threads.h>
#endif

#define KECCAK_X4_CTX_ALIGNMENT 32
#define _KECCAK_X4_CTX_BYTES (800+sizeof(uint64_t))
#define KECCAK_X4_CTX_BYTES (KECCAK_X4_CTX_ALIGNMENT * \
((_KECCAK_X4_CTX_BYTES + KECCAK_X4_CTX_ALIGNMENT - 1)/KECCAK_X4_CTX_ALIGNMENT))

#ifndef __STDC_NO_THREADS__
static once_flag dispatch_once_flag = ONCE_FLAG_INIT;
#ifdef OQS_SHA3_XKCP_DISPATCH_USE_PTHREAD
static pthread_once_t dispatch_once_control = PTHREAD_ONCE_INIT;
#endif

static KeccakX4InitFn *Keccak_X4_Initialize_ptr = NULL;
Expand Down Expand Up @@ -65,8 +65,8 @@ static void Keccak_X4_Dispatch() {
}

static void keccak_x4_inc_reset(uint64_t *s) {
#ifndef __STDC_NO_THREADS__
call_once(&dispatch_once_flag, Keccak_X4_Dispatch);
#ifdef OQS_SHA3_XKCP_DISPATCH_USE_PTHREAD
pthread_once(&dispatch_once_control, Keccak_X4_Dispatch);
#else
if (Keccak_X4_Initialize_ptr == NULL) {
Keccak_X4_Dispatch();
Expand Down

0 comments on commit 8aa10f5

Please sign in to comment.