diff --git a/libkc3/buf.c b/libkc3/buf.c
index ee65738..ca040d8 100644
--- a/libkc3/buf.c
+++ b/libkc3/buf.c
@@ -16,6 +16,7 @@
#include "buf.h"
#include "buf_save.h"
#include "character.h"
+#include "config.h"
#include "error.h"
#include "pretty.h"
#include "ratio.h"
@@ -23,6 +24,8 @@
#include "str.h"
#include "sym.h"
+#if HAVE_PTHREAD
+
#define DEF_BUF_PEEK(type) \
sw buf_peek_ ## type (s_buf *buf, type *dest) \
{ \
@@ -100,10 +103,87 @@
return r; \
}
+#else /* HAVE_PTHREAD */
+
+#define DEF_BUF_PEEK(type) \
+ sw buf_peek_ ## type (s_buf *buf, type *dest) \
+ { \
+ sw r; \
+ const sw size = sizeof(type); \
+ assert(buf); \
+ assert(dest); \
+ if (buf->rpos > buf->wpos || \
+ buf->wpos > buf->size) { \
+ err_puts("buf error"); \
+ assert(! "buf error"); \
+ r = -1; \
+ goto clean; \
+ } \
+ if (buf->rpos + size > buf->wpos) { \
+ if ((r = buf_refill(buf, size)) < 0) \
+ goto clean; \
+ if (r < size) { \
+ r = -1; \
+ goto clean; \
+ } \
+ } \
+ if (buf->rpos + size > buf->wpos) { \
+ assert(! "buf_peek_" # type ": buffer overflow"); \
+ r = -1; \
+ goto clean; \
+ } \
+ *dest = *((type *) (buf->ptr.pchar + buf->rpos)); \
+ r = size; \
+ clean: \
+ return r; \
+ }
+
+#define DEF_BUF_READ(type) \
+ sw buf_read_ ## type (s_buf *buf, type *dest) \
+ { \
+ sw r; \
+ assert(buf); \
+ assert(dest); \
+ r = buf_peek_ ## type(buf, dest); \
+ if (r > 0) { \
+ assert(r == sizeof(type)); \
+ buf->rpos += r; \
+ } \
+ return r; \
+ }
+
+#define DEF_BUF_WRITE(type) \
+ sw buf_write_ ## type (s_buf *buf, type src) \
+ { \
+ sw r; \
+ const sw size = sizeof(type); \
+ assert(buf); \
+ if (buf->wpos + size > buf->size && \
+ buf_flush(buf) < size) { \
+ r = -1; \
+ goto clean; \
+ } \
+ if (buf->wpos + size > buf->size) { \
+ err_puts("buf_write_" # type ": buffer overflow"); \
+ assert(! "buf_write_" # type ": buffer overflow"); \
+ r = -1; \
+ goto clean; \
+ } \
+ *((type *) (buf->ptr.pu8 + buf->wpos)) = src; \
+ buf->wpos += size; \
+ r = size; \
+ clean: \
+ return r; \
+ }
+
+#endif /* HAVE_PTHREAD */
+
void buf_clean (s_buf *buf)
{
assert(buf);
+#if HAVE_PTHREAD
rwlock_clean(&buf->rwlock);
+#endif
if (buf->free)
free(buf->ptr.p);
}
@@ -117,10 +197,14 @@ void buf_delete (s_buf *buf)
void buf_empty (s_buf *buf)
{
+#if HAVE_PTHREAD
rwlock_w(&buf->rwlock);
+#endif
buf->wpos = 0;
buf->rpos = 0;
+#if HAVE_PTHREAD
rwlock_unlock_w(&buf->rwlock);
+#endif
}
sw buf_f (s_buf *buf, const char *fmt, ...)
@@ -150,7 +234,9 @@ sw buf_ignore (s_buf *buf, uw size)
assert(buf);
if (size == 0)
return 0;
+#if HAVE_PTHREAD
rwlock_w(&buf->rwlock);
+#endif
if ((r = buf_refill(buf, size)) < 0) {
err_puts("buf_ignore: buf_refill");
goto clean;
@@ -186,7 +272,9 @@ sw buf_ignore (s_buf *buf, uw size)
assert(i == size);
r = size;
clean:
- rwlock_unlock_w(&buf->rwlock);
+#if HAVE_PTHREAD
+ rwlock_unlock_w(&buf->rwlock);
+#endif
return r;
}
@@ -196,7 +284,9 @@ sw buf_ignore_line (s_buf *buf)
sw csize;
sw r;
sw result = 0;
+#if HAVE_PTHREAD
rwlock_w(&buf->rwlock);
+#endif
while ((r = buf_peek_character_utf8(buf, &c)) > 0) {
csize = r;
if ((r = buf_ignore(buf, csize)) < 0)
@@ -209,7 +299,9 @@ sw buf_ignore_line (s_buf *buf)
goto clean;
r = result;
clean:
+#if HAVE_PTHREAD
rwlock_unlock_w(&buf->rwlock);
+#endif
return result;
}
@@ -221,7 +313,9 @@ sw buf_ignore_newline (s_buf *buf)
sw result = 0;
s_buf_save save;
assert(buf);
+#if HAVE_PTHREAD
rwlock_w(&buf->rwlock);
+#endif
buf_save_init(buf, &save);
while ((r = buf_peek_character_utf8(buf, &c)) > 0 &&
character_is_space(c)) {
@@ -238,7 +332,9 @@ sw buf_ignore_newline (s_buf *buf)
r = 0;
clean:
buf_save_clean(buf, &save);
+#if HAVE_PTHREAD
rwlock_unlock_w(&buf->rwlock);
+#endif
return r;
}
@@ -249,7 +345,9 @@ sw buf_ignore_spaces (s_buf *buf)
sw r;
sw result = 0;
assert(buf);
+#if HAVE_PTHREAD
rwlock_w(&buf->rwlock);
+#endif
while ((r = buf_peek_character_utf8(buf, &c)) > 0 &&
character_is_space(c)) {
csize = r;
@@ -261,7 +359,9 @@ sw buf_ignore_spaces (s_buf *buf)
goto clean;
r = result;
clean:
+#if HAVE_PTHREAD
rwlock_unlock_w(&buf->rwlock);
+#endif
return r;
}
@@ -272,7 +372,9 @@ sw buf_ignore_spaces_but_newline (s_buf *buf)
sw r;
sw result = 0;
assert(buf);
+#if HAVE_PTHREAD
rwlock_w(&buf->rwlock);
+#endif
while ((r = buf_peek_character_utf8(buf, &c)) > 0 &&
character_is_space(c) &&
c != '\n') {
@@ -283,7 +385,9 @@ sw buf_ignore_spaces_but_newline (s_buf *buf)
}
r = result;
clean:
+#if HAVE_PTHREAD
rwlock_unlock_w(&buf->rwlock);
+#endif
return r;
}
@@ -295,7 +399,9 @@ s_buf * buf_init (s_buf *buf, bool p_free, uw size, char *p)
tmp.line = 1;
tmp.ptr.pchar = p;
tmp.size = size;
+#if HAVE_PTHREAD
rwlock_init(&tmp.rwlock);
+#endif
*buf = tmp;
return buf;
}
@@ -308,7 +414,9 @@ s_buf * buf_init_const (s_buf *buf, uw size, const char *p)
tmp.ptr.pchar = (char *) p;
tmp.read_only = true;
tmp.size = size;
+#if HAVE_PTHREAD
rwlock_init(&tmp.rwlock);
+#endif
*buf = tmp;
return buf;
}
@@ -440,7 +548,9 @@ sw buf_peek_next_character_utf8 (s_buf *buf, character *c)
character tmp;
assert(buf);
assert(c);
+#if HAVE_PTHREAD
rwlock_w(&buf->rwlock);
+#endif
buf_save_init(buf, &save);
if ((r = buf_read_character_utf8(buf, &tmp)) <= 0)
goto clean;
@@ -454,7 +564,9 @@ sw buf_peek_next_character_utf8 (s_buf *buf, character *c)
buf_save_restore_rpos(buf, &save);
clean:
buf_save_clean(buf, &save);
+#if HAVE_PTHREAD
rwlock_unlock_w(&buf->rwlock);
+#endif
return r;
}
@@ -474,7 +586,9 @@ sw buf_peek_character_utf8 (s_buf *buf, character *c)
const u8 _11100000 = 0xE0;
const u8 _11110000 = 0xF0;
const u8 _11111000 = 0xF8;
+#if HAVE_PTHREAD
rwlock_w(&buf->rwlock);
+#endif
if ((r = buf_refill(buf, 1)) < 0)
goto clean;
if (r < 1) {
@@ -555,7 +669,9 @@ sw buf_peek_character_utf8 (s_buf *buf, character *c)
}
r = 0;
clean:
+#if HAVE_PTHREAD
rwlock_unlock_w(&buf->rwlock);
+#endif
return r;
}
@@ -575,7 +691,9 @@ sw buf_peek_str (s_buf *buf, const s_str *src)
assert(src);
if (! src->size)
return 0;
+#if HAVE_PTHREAD
rwlock_w(&buf->rwlock);
+#endif
if (buf->rpos > buf->wpos) {
err_puts("buf_peek_str: buf error: rpos > wpos");
assert(! "buf_peek_str: buf error: rpos > wpos");
@@ -606,7 +724,9 @@ sw buf_peek_str (s_buf *buf, const s_str *src)
}
r = src->size;
clean:
+#if HAVE_PTHREAD
rwlock_unlock_w(&buf->rwlock);
+#endif
return r;
}
@@ -618,7 +738,9 @@ sw buf_peek_to_str (s_buf *buf, s_str *dest)
sw size;
assert(buf);
assert(dest);
+#if HAVE_PTHREAD
rwlock_w(&buf->rwlock);
+#endif
if (buf->rpos > buf->wpos) {
r = -1;
goto clean;
@@ -636,7 +758,9 @@ sw buf_peek_to_str (s_buf *buf, s_str *dest)
str_init_alloc_copy(dest, size, buf->ptr.pchar + buf->rpos);
r = size;
clean:
+#if HAVE_PTHREAD
rwlock_unlock_w(&buf->rwlock);
+#endif
return r;
}
@@ -655,7 +779,9 @@ s_str * buf_read (s_buf *buf, uw size, s_str *dest)
assert(buf);
if (! size)
return str_init_empty(dest);
+#if HAVE_PTHREAD
rwlock_w(&buf->rwlock);
+#endif
if (buf->rpos > buf->wpos)
goto clean;
if (buf->rpos + size > buf->wpos &&
@@ -674,7 +800,9 @@ s_str * buf_read (s_buf *buf, uw size, s_str *dest)
*dest = tmp;
result = dest;
clean:
+#if HAVE_PTHREAD
rwlock_unlock_w(&buf->rwlock);
+#endif
return result;
}
@@ -689,7 +817,9 @@ sw buf_read_1 (s_buf *buf, const char *p)
sw buf_read_character_utf8 (s_buf *buf, character *p)
{
sw r;
+#if HAVE_PTHREAD
rwlock_w(&buf->rwlock);
+#endif
r = buf_peek_character_utf8(buf, p);
if (r > 0) {
if (buf->line >= 0) {
@@ -702,7 +832,9 @@ sw buf_read_character_utf8 (s_buf *buf, character *p)
}
buf->rpos += r;
}
+#if HAVE_PTHREAD
rwlock_unlock_w(&buf->rwlock);
+#endif
return r;
}
@@ -717,11 +849,15 @@ DEF_BUF_READ(s64)
sw buf_read_str (s_buf *buf, const s_str *src)
{
sw r;
+#if HAVE_PTHREAD
rwlock_w(&buf->rwlock);
+#endif
r = buf_peek_str(buf, src);
if (r > 0)
r = buf_ignore(buf, r);
+#if HAVE_PTHREAD
rwlock_unlock_w(&buf->rwlock);
+#endif
return r;
}
@@ -732,7 +868,9 @@ sw buf_read_sym (s_buf *buf, const s_sym *src)
character c;
sw r;
s_buf_save save;
+#if HAVE_PTHREAD
rwlock_w(&buf->rwlock);
+#endif
buf_save_init(buf, &save);
r = buf_read_str(buf, &src->str);
if (r > 0 &&
@@ -745,7 +883,9 @@ sw buf_read_sym (s_buf *buf, const s_sym *src)
buf_save_restore_rpos(buf, &save);
clean:
buf_save_clean(buf, &save);
+#if HAVE_PTHREAD
rwlock_unlock_w(&buf->rwlock);
+#endif
return r;
}
@@ -757,7 +897,9 @@ s_str * buf_read_to_str (s_buf *buf, s_str *dest)
s_str tmp = {0};
assert(buf);
assert(dest);
+#if HAVE_PTHREAD
rwlock_w(&buf->rwlock);
+#endif
if (buf->rpos > buf->wpos) {
err_puts("buf_read_to_str: buf->rpos > buf->wpos");
assert(! "buf_read_to_str: buf->rpos > buf->wpos");
@@ -789,7 +931,9 @@ s_str * buf_read_to_str (s_buf *buf, s_str *dest)
*dest = tmp;
result = dest;
clean:
+#if HAVE_PTHREAD
rwlock_unlock_w(&buf->rwlock);
+#endif
return result;
}
@@ -824,7 +968,9 @@ sw buf_read_until_space_into_str (s_buf *buf, s_str *dest)
sw result = 0;
s_buf_save save;
s_buf tmp;
+#if HAVE_PTHREAD
rwlock_w(&buf->rwlock);
+#endif
buf_save_init(buf, &save);
while (1) {
if ((r = buf_peek_character_utf8(buf, &c)) < 0)
@@ -848,7 +994,9 @@ sw buf_read_until_space_into_str (s_buf *buf, s_str *dest)
buf_save_restore_rpos(buf, &save);
clean:
buf_save_clean(buf, &save);
+#if HAVE_PTHREAD
rwlock_unlock_w(&buf->rwlock);
+#endif
return r;
}
@@ -860,7 +1008,9 @@ s_str * buf_read_until_str_into_str (s_buf *buf, const s_str *end,
s_str *result = NULL;
s_buf_save save;
s_buf tmp;
+#if HAVE_PTHREAD
rwlock_w(&buf->rwlock);
+#endif
buf_save_init(buf, &save);
while (1) {
if ((r = buf_read_str(buf, end)) < 0) {
@@ -889,7 +1039,9 @@ s_str * buf_read_until_str_into_str (s_buf *buf, const s_str *end,
buf_save_restore_rpos(buf, &save);
clean:
buf_save_clean(buf, &save);
+#if HAVE_PTHREAD
rwlock_unlock_w(&buf->rwlock);
+#endif
return result;
}
@@ -899,7 +1051,9 @@ sw buf_refill (s_buf *buf, sw size)
{
sw r;
assert(buf);
+#if HAVE_PTHREAD
rwlock_w(&buf->rwlock);
+#endif
r = buf->wpos - buf->rpos;
if (size < 0) {
err_puts("buf_refill: size < 0");
@@ -927,7 +1081,9 @@ sw buf_refill (s_buf *buf, sw size)
}
r = buf->wpos - buf->rpos;
clean:
+#if HAVE_PTHREAD
rwlock_unlock_w(&buf->rwlock);
+#endif
return r;
}
@@ -940,7 +1096,9 @@ sw buf_refill_compact (s_buf *buf)
assert(buf);
if (buf->read_only)
return 0;
+#if HAVE_PTHREAD
rwlock_w(&buf->rwlock);
+#endif
min_rpos = buf_save_min_rpos(buf);
if (min_rpos > buf->wpos) {
assert(min_rpos <= buf->wpos);
@@ -981,7 +1139,9 @@ sw buf_refill_compact (s_buf *buf)
}
r = 1;
clean:
+#if HAVE_PTHREAD
rwlock_unlock_w(&buf->rwlock);
+#endif
return r;
}
@@ -992,7 +1152,9 @@ s_str * buf_slice_to_str (s_buf *buf, uw start, uw end,
s_str tmp;
assert(buf);
assert(dest);
+#if HAVE_PTHREAD
rwlock_r(&buf->rwlock);
+#endif
if (start > buf->wpos) {
err_puts("buf_slice_to_str: start > wpos");
assert(! "buf_slice_to_str: start > wpos");
@@ -1011,7 +1173,9 @@ s_str * buf_slice_to_str (s_buf *buf, uw start, uw end,
str_init(&tmp, NULL, end - start, buf->ptr.pchar + start);
result = str_init_copy(dest, &tmp);
clean:
+#if HAVE_PTHREAD
rwlock_unlock_r(&buf->rwlock);
+#endif
return result;
}
@@ -1021,7 +1185,9 @@ sw buf_str_to_hex (s_buf *buf, const s_str *src)
sw r;
sw size;
uw i;
+#if HAVE_PTHREAD
rwlock_w(&buf->rwlock);
+#endif
if (src->size == 0) {
r = 0;
goto clean;
@@ -1039,7 +1205,9 @@ sw buf_str_to_hex (s_buf *buf, const s_str *src)
goto clean;
r = size;
clean:
+#if HAVE_PTHREAD
rwlock_unlock_w(&buf->rwlock);
+#endif
return r;
}
@@ -1060,7 +1228,9 @@ sw buf_u8_to_hex (s_buf *buf, const u8 *x)
sw r;
sw result = 0;
s_buf_save save;
+#if HAVE_PTHREAD
rwlock_w(&buf->rwlock);
+#endif
buf_save_init(buf, &save);
digit = *x >> 4;
if (digit < 10)
@@ -1084,7 +1254,9 @@ sw buf_u8_to_hex (s_buf *buf, const u8 *x)
buf_save_restore_wpos(buf, &save);
clean:
buf_save_clean(buf, &save);
+#if HAVE_PTHREAD
rwlock_unlock_w(&buf->rwlock);
+#endif
return r;
}
@@ -1110,7 +1282,9 @@ sw buf_write (s_buf *buf, const void *data, uw len)
assert(buf);
if (! len)
return 0;
+#if HAVE_PTHREAD
rwlock_w(&buf->rwlock);
+#endif
if (buf->wpos > buf->size) {
err_puts("buf_write: buf error: wpos > size");
assert(! "buf_write: buf error: wpos > size");
@@ -1133,7 +1307,9 @@ sw buf_write (s_buf *buf, const void *data, uw len)
buf_flush(buf);
r = len;
clean:
+#if HAVE_PTHREAD
rwlock_unlock_w(&buf->rwlock);
+#endif
return r;
}
@@ -1163,7 +1339,9 @@ sw buf_write_character_utf8 (s_buf *buf, character c)
csize = character_utf8_size(c);
if (csize <= 0)
return csize;
+#if HAVE_PTHREAD
rwlock_w(&buf->rwlock);
+#endif
if (c == '\n')
size = csize + buf->pretty.base_column;
else
@@ -1189,7 +1367,9 @@ sw buf_write_character_utf8 (s_buf *buf, character c)
buf->wpos += size;
r = size;
clean:
+#if HAVE_PTHREAD
rwlock_unlock_w(&buf->rwlock);
+#endif
return r;
}
@@ -1228,7 +1408,9 @@ sw buf_write_str (s_buf *buf, const s_str *src)
assert(buf);
assert(src);
s = *src;
+#if HAVE_PTHREAD
rwlock_w(&buf->rwlock);
+#endif
while ((r = str_read_character_utf8(&s, &c)) > 0) {
if ((r = buf_write_character_utf8(buf, c)) < 0)
goto clean;
@@ -1237,7 +1419,9 @@ sw buf_write_str (s_buf *buf, const s_str *src)
buf_flush(buf);
r = result;
clean:
+#if HAVE_PTHREAD
rwlock_unlock_w(&buf->rwlock);
+#endif
return r;
}
@@ -1262,12 +1446,16 @@ sw buf_write_str_without_indent (s_buf *buf, const s_str *src)
{
s_pretty_save pretty_save;
sw r;
+#if HAVE_PTHREAD
rwlock_w(&buf->rwlock);
+#endif
pretty_save_init(&pretty_save, &buf->pretty);
pretty_indent_at_column(&buf->pretty, 0);
r = buf_write_str(buf, src);
pretty_save_clean(&pretty_save, &buf->pretty);
+#if HAVE_PTHREAD
rwlock_unlock_w(&buf->rwlock);
+#endif
return r;
}
@@ -1297,8 +1485,10 @@ sw buf_xfer (s_buf *dest, s_buf *src, uw size)
assert(src);
if (size == 0)
return 0;
+#if HAVE_PTHREAD
rwlock_w(&dest->rwlock);
rwlock_w(&src->rwlock);
+#endif
assert(src->rpos <= src->wpos);
assert(dest->rpos <= dest->wpos);
if (src->rpos + size > src->wpos &&
@@ -1320,8 +1510,10 @@ sw buf_xfer (s_buf *dest, s_buf *src, uw size)
dest->wpos += size;
r = size;
clean:
+#if HAVE_PTHREAD
rwlock_unlock_w(&src->rwlock);
rwlock_unlock_w(&dest->rwlock);
+#endif
return r;
}
@@ -1331,8 +1523,10 @@ sw buf_xfer_reverse (s_buf *src, s_buf *dest)
sw size;
assert(src);
assert(dest);
+#if HAVE_PTHREAD
rwlock_w(&src->rwlock);
rwlock_w(&dest->rwlock);
+#endif
size = src->wpos - src->rpos;
if (dest->wpos + size > dest->size &&
buf_flush(dest) < size) {
@@ -1345,7 +1539,9 @@ sw buf_xfer_reverse (s_buf *src, s_buf *dest)
dest->wpos += size;
r = size;
clean:
+#if HAVE_PTHREAD
rwlock_unlock_w(&dest->rwlock);
rwlock_unlock_w(&src->rwlock);
+#endif
return r;
}
diff --git a/libkc3/callable.c b/libkc3/callable.c
index 102e474..888c40f 100644
--- a/libkc3/callable.c
+++ b/libkc3/callable.c
@@ -22,7 +22,9 @@
void callable_delete (s_callable *callable)
{
assert(callable);
+#if HAVE_PTHREAD
mutex_lock(&callable->mutex);
+#endif
if (callable->reference_count <= 0)
goto clean;
if (--callable->reference_count > 0)
@@ -32,12 +34,17 @@ void callable_delete (s_callable *callable)
case CALLABLE_FN: fn_clean(&callable->data.fn); break;
case CALLABLE_VOID: break;
}
+#if HAVE_PTHREAD
mutex_unlock(&callable->mutex);
mutex_clean(&callable->mutex);
+#endif
free(callable);
return;
clean:
+#if HAVE_PTHREAD
mutex_unlock(&callable->mutex);
+#endif
+ return;
}
s_callable * callable_new (void)
@@ -45,7 +52,9 @@ s_callable * callable_new (void)
s_callable *callable;
if (! (callable = alloc(sizeof(s_callable))))
return NULL;
+#if HAVE_PTHREAD
mutex_init(&callable->mutex);
+#endif
return callable;
}
@@ -77,14 +86,18 @@ s_callable * callable_new_copy (s_callable *src)
s_callable * callable_new_ref (s_callable *callable)
{
assert(callable);
+#if HAVE_PTHREAD
mutex_lock(&callable->mutex);
+#endif
if (callable->reference_count <= 0) {
err_puts("callable_new_ref: reference count <= 0");
assert(! "callable_new_ref: reference count <= 0");
abort();
}
callable->reference_count++;
+#if HAVE_PTHREAD
mutex_unlock(&callable->mutex);
+#endif
return callable;
}
diff --git a/libkc3/cfn.c b/libkc3/cfn.c
index 9556417..b404473 100644
--- a/libkc3/cfn.c
+++ b/libkc3/cfn.c
@@ -188,7 +188,9 @@ void cfn_clean (s_cfn *cfn)
list_delete_all(cfn->arg_types);
if (cfn->cif.nargs)
free(cfn->cif.arg_types);
+#if HAVE_PTHREAD
mutex_clean(&cfn->mutex);
+#endif
}
void cfn_delete (s_cfn *cfn)
@@ -201,7 +203,9 @@ void cfn_delete (s_cfn *cfn)
bool cfn_eval (s_cfn *cfn)
{
assert(cfn);
+#if HAVE_PTHREAD
mutex_lock(&cfn->mutex);
+#endif
if (! cfn->ready) {
if (! cfn_prep_cif(cfn))
goto ko;
@@ -209,10 +213,14 @@ bool cfn_eval (s_cfn *cfn)
goto ko;
cfn->ready = true;
}
+#if HAVE_PTHREAD
mutex_unlock(&cfn->mutex);
+#endif
return true;
ko:
+#if HAVE_PTHREAD
mutex_unlock(&cfn->mutex);
+#endif
return false;
}
@@ -232,7 +240,9 @@ s_cfn * cfn_init (s_cfn *cfn, const s_sym *name, s_list *arg_types,
}
tmp.arity = arity;
tmp.result_type = result_type;
+#if HAVE_PTHREAD
mutex_init(&tmp.mutex);
+#endif
*cfn = tmp;
return cfn;
}
@@ -287,7 +297,9 @@ s_cfn * cfn_init_copy (s_cfn *cfn, const s_cfn *src)
tmp.ptr = src->ptr;
tmp.macro = src->macro;
tmp.special_operator = src->special_operator;
+#if HAVE_PTHREAD
mutex_init(&tmp.mutex);
+#endif
*cfn = tmp;
return cfn;
}
@@ -301,7 +313,9 @@ s_cfn * cfn_link (s_cfn *cfn)
err_write_1(": ");
err_puts(dlerror());
assert(! "cfn_link: dlsym failed");
+#if HAVE_PTHREAD
mutex_unlock(&cfn->mutex);
+#endif
return NULL;
}
return cfn;
diff --git a/libkc3/facts_with.c b/libkc3/facts_with.c
index 99cd4fc..cc7fdf1 100644
--- a/libkc3/facts_with.c
+++ b/libkc3/facts_with.c
@@ -51,7 +51,9 @@ s_facts_with_cursor * facts_with (s_facts *facts,
}
/* facts_spec_sort(tmp.spec); */
}
+#if HAVE_PTHREAD
mutex_init(&tmp.mutex);
+#endif
*cursor = tmp;
return cursor;
}
diff --git a/libkc3/facts_with_cursor.c b/libkc3/facts_with_cursor.c
index b7d85e1..deb270e 100644
--- a/libkc3/facts_with_cursor.c
+++ b/libkc3/facts_with_cursor.c
@@ -34,7 +34,9 @@ void facts_with_cursor_clean (s_facts_with_cursor *cursor)
}
free(cursor->levels);
free(cursor->spec);
+#if HAVE_PTHREAD
mutex_clean(&cursor->mutex);
+#endif
}
}
@@ -49,7 +51,9 @@ s_fact ** facts_with_cursor_next (s_facts_with_cursor *cursor,
*dest = NULL;
return dest;
}
+#if HAVE_PTHREAD
mutex_lock(&cursor->mutex);
+#endif
if (cursor->level == cursor->facts_count) {
level = &cursor->levels[cursor->facts_count - 1];
#ifdef DEBUG_FACTS
@@ -70,7 +74,9 @@ s_fact ** facts_with_cursor_next (s_facts_with_cursor *cursor,
#endif
if (level->fact) {
*dest = level->fact;
+#if HAVE_PTHREAD
mutex_unlock(&cursor->mutex);
+#endif
return dest;
}
free(level->spec);
@@ -120,18 +126,24 @@ s_fact ** facts_with_cursor_next (s_facts_with_cursor *cursor,
goto not_found;
cursor->level--;
}
+#if HAVE_PTHREAD
mutex_unlock(&cursor->mutex);
+#endif
*dest = fact;
return dest;
not_found:
cursor->facts_count = 0;
free(cursor->levels);
free(cursor->spec);
+#if HAVE_PTHREAD
mutex_unlock(&cursor->mutex);
mutex_clean(&cursor->mutex);
+#endif
*dest = NULL;
return dest;
ko:
+#if HAVE_PTHREAD
mutex_unlock(&cursor->mutex);
+#endif
return NULL;
}
diff --git a/libkc3/mutex.h b/libkc3/mutex.h
index ae78fab..0733177 100644
--- a/libkc3/mutex.h
+++ b/libkc3/mutex.h
@@ -21,6 +21,8 @@
#include "types.h"
+#if HAVE_PTHREAD
+
/* Stack-allocation compatible functions, call mutex_clean after use. */
void mutex_clean (s_mutex *mutex);
s_mutex * mutex_init (s_mutex *mutex);
@@ -33,4 +35,6 @@ s_mutex * mutex_new (void);
void mutex_lock (s_mutex *mutex);
void mutex_unlock (s_mutex *mutex);
+#endif /* HAVE_PTHREAD */
+
#endif /* LIBKC3_MUTEX_H */
diff --git a/libkc3/rwlock.h b/libkc3/rwlock.h
index bf983ed..7a9938a 100644
--- a/libkc3/rwlock.h
+++ b/libkc3/rwlock.h
@@ -15,6 +15,8 @@
#include "types.h"
+#if HAVE_PTHREAD
+
/* Stack-allocation compatible functions, call rwlock_clean
after use. */
void rwlock_clean (s_rwlock *rwlock);
@@ -26,4 +28,6 @@ s_rwlock * rwlock_unlock_r (s_rwlock *rwlock);
s_rwlock * rwlock_unlock_w (s_rwlock *rwlock);
s_rwlock * rwlock_w (s_rwlock *rwlock);
+#endif /* HAVE_PTHREAD */
+
#endif /* LIBKC3_RWLOCK_H */
diff --git a/libkc3/struct_type.c b/libkc3/struct_type.c
index 924a29f..ba26580 100644
--- a/libkc3/struct_type.c
+++ b/libkc3/struct_type.c
@@ -160,7 +160,11 @@ s_struct_type * struct_type_init (s_struct_type *st,
if (sizeof(long) == 4)
tmp.size = (offset + 3) / 4 * 4;
else
+#ifdef __APPLE__
+ tmp.size = (offset + 7) / 8 * 8;
+#else
tmp.size = (offset + 15) / 16 * 16;
+#endif
*st = tmp;
return st;
}