Skip to content

Commit 3a3dabd

Browse files
committed
all: Make multithreading support configurable.
This is useful in cases where multi threading is not used. It is also currently necessary to turn off threading when compiling with MSVC as that compiler does not have pthreads. While here, remove the broken CSNIP_CONF__EMULATE_{READ,WRITE}V macros from the configuration file; they're not working.
1 parent 6923f4c commit 3a3dabd

File tree

4 files changed

+34
-25
lines changed

4 files changed

+34
-25
lines changed

CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,16 @@ project(csnip
88
# Add module path
99
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
1010

11-
find_package(Threads REQUIRED)
11+
# Find threading library and enable thread safety if applicable
12+
find_package(Threads)
13+
if (Threads_FOUND)
14+
set(_dflt_support_threading ON)
15+
else()
16+
set(_dflt_support_threading OFF)
17+
endif()
18+
option(SUPPORT_THREADING
19+
"Make some modules multithreading safe"
20+
${_dflt_support_threading})
1221

1322
include(EnableWarnings)
1423
include(CxxSetup)

src/csnip/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
### Configure csnip
22

3-
## Step 1: Feature/Environment checks
3+
## Step 1: User preferences
4+
5+
set(CSNIP_CONF__SUPPORT_THREADING ${SUPPORT_THREADING})
6+
7+
## Step 2: Feature/Environment checks
48

59
# Check for various types & APIs in libc and provided
610
# libraries.
@@ -166,7 +170,7 @@ set(c_sources
166170
)
167171

168172
set(csnip_link_libs
169-
Threads::Threads
173+
$<$<BOOL:${SUPPORT_THREADING}>:Threads::Threads>
170174
${LINK_LIBM}
171175
)
172176

src/csnip/csnip_conf.h.in

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,9 @@
2020
* will be provided.
2121
*/
2222

23-
/** Use csnip's emulated readv() implementation.
24-
*
25-
* If this option is disabled, csnip will use the system readv()
26-
* for the implementation of csnip_x_readv(); if there is no system
27-
* readv(), then the function will not be available in this case.
28-
*
29-
* If the option is enabled, csnip's own implementation based on read()
30-
* is used. (That implementation is typically less efficient than the
31-
* system one.)
32-
*
33-
* By default this option is disabled if readv() is available, and
34-
* enabled if it isn't.
35-
*/
36-
#cmakedefine CSNIP_CONF__EMULATE_READV
37-
38-
/** Use csnip's emulated writev() implementation.
39-
*
40-
* Anologuous to the similar setting for readv().
41-
* @sa CSNIP_CONF__EMULATE_READV
42-
*/
43-
#cmakedefine CSNIP_CONF__EMULATE_WRITEV
23+
#cmakedefine CSNIP_CONF__SUPPORT_THREADING
4424

45-
/** @}
25+
/** @def
4626
*/
4727

4828
/** @defgroup conf_env Environment properties

src/csnip/log.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
#ifdef CSNIP_CONF__HAVE_REGCOMP
1212
#include <regex.h>
1313
#endif
14+
#ifdef CSNIP_CONF__SUPPORT_THREADING
1415
#include <pthread.h>
16+
#endif
1517

1618
#define CSNIP_SHORT_NAMES
1719
#include <csnip/cext.h>
@@ -72,7 +74,9 @@ typedef struct {
7274
*
7375
* This access lock protects access to ptbl.
7476
*/
77+
#ifdef CSNIP_CONF__SUPPORT_THREADING
7578
pthread_rwlock_t lock;
79+
#endif
7680

7781
/** Priorities cache */
7882
struct priotbl* ptbl;
@@ -109,7 +113,9 @@ static void proc_init(csnip_log_processor* P)
109113
P->rules_head = P->rules_tail = NULL;
110114
P->min_prio = 100;
111115
int err = 0;
116+
#ifdef CSNIP_CONF__SUPPORT_THREADING
112117
pthread_rwlock_init(&P->lock, NULL);
118+
#endif
113119
P->ptbl = ptbl_make(&err); // error handling
114120
for (int i = 0; i < Static_len(P->logfmt); ++i)
115121
P->logfmt[i] = NULL;
@@ -134,7 +140,9 @@ static void proc_free(csnip_log_processor* P)
134140
}
135141

136142
/* Free lock */
143+
#ifdef CSNIP_CONF__SUPPORT_THREADING
137144
pthread_rwlock_destroy(&P->lock);
145+
#endif
138146

139147
/* Free the hashing table */
140148
ptbl_free(P->ptbl);
@@ -419,9 +427,13 @@ void csnip_log__print(
419427
csnip_log_processor* P = proc;
420428

421429
/* Check whether to display */
430+
#ifdef CSNIP_CONF__SUPPORT_THREADING
422431
pthread_rwlock_rdlock(&P->lock);
432+
#endif
423433
comp_prio* cp = ptbl_find(proc->ptbl, component);
434+
#ifdef CSNIP_CONF__SUPPORT_THREADING
424435
pthread_rwlock_unlock(&P->lock);
436+
#endif
425437
if (cp != NULL) {
426438
if (prio < cp->min_prio)
427439
return;
@@ -451,9 +463,13 @@ void csnip_log__print(
451463
.component = component,
452464
.min_prio = comp_min_prio
453465
};
466+
#ifdef CSNIP_CONF__SUPPORT_THREADING
454467
pthread_rwlock_wrlock(&P->lock);
468+
#endif
455469
ptbl_insert(P->ptbl, NULL, Pent);
470+
#ifdef CSNIP_CONF__SUPPORT_THREADING
456471
pthread_rwlock_unlock(&P->lock);
472+
#endif
457473

458474
/* Check if we should display */
459475
if (prio < Pent.min_prio)

0 commit comments

Comments
 (0)