make all library-internal static buffers thread-local
We have a number of library-internal static global buffers which are
mainly used for various stringification functions. This worked as
all of the related Osmocom programs were strictly single-threaded.
Let's make those buffers at least thread-local. This way every thread
gets their own set of buffers, and it's safe for multiple threads to
execute the same functions once. They're of course still not
re-entrant. If you need re-entrancy, you will need to use the _c()
or _buf() suffix version of those functions and work with your own
(stack or heap) buffers.
Change-Id: I50eb2436a7c1261d79a9d2955584dce92780ca07
diff --git a/src/fsm.c b/src/fsm.c
index 882a2b4..411797d 100644
--- a/src/fsm.c
+++ b/src/fsm.c
@@ -545,7 +545,7 @@
*/
const char *osmo_fsm_event_name(struct osmo_fsm *fsm, uint32_t event)
{
- static char buf[32];
+ static __thread char buf[32];
if (!fsm->event_names) {
snprintf(buf, sizeof(buf), "%"PRIu32, event);
return buf;
@@ -575,7 +575,7 @@
*/
const char *osmo_fsm_state_name(struct osmo_fsm *fsm, uint32_t state)
{
- static char buf[32];
+ static __thread char buf[32];
if (state >= fsm->num_states) {
snprintf(buf, sizeof(buf), "unknown %"PRIu32, state);
return buf;
diff --git a/src/gb/gprs_ns.c b/src/gb/gprs_ns.c
index cee4885..c77ebb3 100644
--- a/src/gb/gprs_ns.c
+++ b/src/gb/gprs_ns.c
@@ -1552,7 +1552,7 @@
const char *gprs_ns_ll_str(const struct gprs_nsvc *nsvc)
{
- static char buf[80];
+ static __thread char buf[80];
return gprs_ns_ll_str_buf(buf, sizeof(buf), nsvc);
}
diff --git a/src/gsm/abis_nm.c b/src/gsm/abis_nm.c
index a4c0e41..3fb8f0f 100644
--- a/src/gsm/abis_nm.c
+++ b/src/gsm/abis_nm.c
@@ -939,7 +939,7 @@
const char *abis_nm_dump_foh(const struct abis_om_fom_hdr *foh)
{
- static char foh_buf[128];
+ static __thread char foh_buf[128];
return abis_nm_dump_foh_buf(foh_buf, sizeof(foh_buf), foh);
}
diff --git a/src/gsm/apn.c b/src/gsm/apn.c
index 88b45a4..a7074ea 100644
--- a/src/gsm/apn.c
+++ b/src/gsm/apn.c
@@ -31,7 +31,7 @@
#define APN_OI_GPRS_FMT "mnc%03u.mcc%03u.gprs"
#define APN_GPRS_FMT "%s.mnc%03u.mcc%03u.gprs"
-static char apn_strbuf[APN_MAXLEN+1];
+static __thread char apn_strbuf[APN_MAXLEN+1];
char *osmo_apn_qualify_buf(char *buf, size_t buf_len, unsigned int mcc, unsigned int mnc, const char *ni)
{
diff --git a/src/gsm/gsm0808_utils.c b/src/gsm/gsm0808_utils.c
index 26f7944..70eed15 100644
--- a/src/gsm/gsm0808_utils.c
+++ b/src/gsm/gsm0808_utils.c
@@ -611,7 +611,7 @@
return ret;
}
-static char dbuf[256];
+static __thread char dbuf[256];
/*! Dump LCLS parameters (GCR excluded) into string for printing.
* \param[out] buf caller-allocated output string buffer
@@ -1819,7 +1819,7 @@
*/
const char *gsm0808_cell_id_name(const struct gsm0808_cell_id *cid)
{
- static char buf[64];
+ static __thread char buf[64];
return gsm0808_cell_id_name_buf(buf, sizeof(buf), cid);
}
@@ -1829,7 +1829,7 @@
*/
const char *gsm0808_cell_id_name2(const struct gsm0808_cell_id *cid)
{
- static char buf[64];
+ static __thread char buf[64];
return gsm0808_cell_id_name_buf(buf, sizeof(buf), cid);
}
@@ -1886,7 +1886,7 @@
* See also gsm0808_cell_id_list_name_buf(). */
const char *gsm0808_cell_id_list_name(const struct gsm0808_cell_id_list2 *cil)
{
- static char buf[1024];
+ static __thread char buf[1024];
gsm0808_cell_id_list_name_buf(buf, sizeof(buf), cil);
return buf;
}
@@ -1913,7 +1913,7 @@
const char *gsm0808_channel_type_name(const struct gsm0808_channel_type *ct)
{
- static char buf[128];
+ static __thread char buf[128];
return gsm0808_channel_type_name_buf(buf, sizeof(buf), ct);
}
diff --git a/src/gsm/gsm23003.c b/src/gsm/gsm23003.c
index a97ed07..4cc2671 100644
--- a/src/gsm/gsm23003.c
+++ b/src/gsm/gsm23003.c
@@ -107,7 +107,7 @@
*/
const char *osmo_mcc_name(uint16_t mcc)
{
- static char buf[8];
+ static __thread char buf[8];
return osmo_mcc_name_buf(buf, sizeof(buf), mcc);
}
@@ -158,7 +158,7 @@
*/
const char *osmo_mnc_name(uint16_t mnc, bool mnc_3_digits)
{
- static char buf[8];
+ static __thread char buf[8];
return osmo_mnc_name_buf(buf, sizeof(buf), mnc, mnc_3_digits);
}
@@ -182,7 +182,7 @@
*/
const char *osmo_plmn_name(const struct osmo_plmn_id *plmn)
{
- static char buf[16];
+ static __thread char buf[16];
return osmo_plmn_name_buf(buf, sizeof(buf), plmn);
}
@@ -193,7 +193,7 @@
*/
const char *osmo_plmn_name2(const struct osmo_plmn_id *plmn)
{
- static char buf[16];
+ static __thread char buf[16];
return osmo_plmn_name_buf(buf, sizeof(buf), plmn);
}
@@ -230,7 +230,7 @@
*/
const char *osmo_lai_name(const struct osmo_location_area_id *lai)
{
- static char buf[32];
+ static __thread char buf[32];
return osmo_lai_name_buf(buf, sizeof(buf), lai);
}
@@ -265,7 +265,7 @@
*/
const char *osmo_cgi_name(const struct osmo_cell_global_id *cgi)
{
- static char buf[32];
+ static __thread char buf[32];
return osmo_cgi_name_buf(buf, sizeof(buf), cgi);
}
@@ -276,7 +276,7 @@
*/
const char *osmo_cgi_name2(const struct osmo_cell_global_id *cgi)
{
- static char buf[32];
+ static __thread char buf[32];
return osmo_cgi_name_buf(buf, sizeof(buf), cgi);
}
@@ -320,7 +320,7 @@
*/
const char *osmo_gummei_name(const struct osmo_gummei *gummei)
{
- static char buf[32];
+ static __thread char buf[32];
return osmo_gummei_name_buf(buf, sizeof(buf), gummei);
}
diff --git a/src/gsm/gsm48.c b/src/gsm/gsm48.c
index c2c19cf..c97337b 100644
--- a/src/gsm/gsm48.c
+++ b/src/gsm/gsm48.c
@@ -203,7 +203,7 @@
*/
const char *osmo_rai_name(const struct gprs_ra_id *rai)
{
- static char buf[32];
+ static __thread char buf[32];
return osmo_rai_name_buf(buf, sizeof(buf), rai);
}
@@ -502,7 +502,7 @@
*/
const char *osmo_mi_name(const uint8_t *mi, uint8_t mi_len)
{
- static char mi_name[10 + GSM48_MI_SIZE + 1];
+ static __thread char mi_name[10 + GSM48_MI_SIZE + 1];
return osmo_mi_name_buf(mi_name, sizeof(mi_name), mi, mi_len);
}
@@ -1150,7 +1150,7 @@
*/
const char *gsm48_pdisc_msgtype_name(uint8_t pdisc, uint8_t msg_type)
{
- static char namebuf[64];
+ static __thread char namebuf[64];
return gsm48_pdisc_msgtype_name_buf(namebuf, sizeof(namebuf), pdisc, msg_type);
}
@@ -1303,7 +1303,7 @@
*/
const char *osmo_gsm48_classmark_a5_name(const struct osmo_gsm48_classmark *cm)
{
- static char buf[128];
+ static __thread char buf[128];
return osmo_gsm48_classmark_a5_name_buf(buf, sizeof(buf), cm);
}
diff --git a/src/gsm/gsm_utils.c b/src/gsm/gsm_utils.c
index 07f053c..f154c2b 100644
--- a/src/gsm/gsm_utils.c
+++ b/src/gsm/gsm_utils.c
@@ -897,7 +897,7 @@
char *osmo_dump_gsmtime(const struct gsm_time *tm)
{
- static char buf[64];
+ static __thread char buf[64];
return osmo_dump_gsmtime_buf(buf, sizeof(buf), tm);
}
diff --git a/src/gsm/rsl.c b/src/gsm/rsl.c
index 0409734..3be0111 100644
--- a/src/gsm/rsl.c
+++ b/src/gsm/rsl.c
@@ -264,7 +264,7 @@
*/
const char *rsl_chan_nr_str(uint8_t chan_nr)
{
- static char str[20];
+ static __thread char str[20];
return rsl_chan_nr_str_buf(str, sizeof(str), chan_nr);
}
diff --git a/src/msgb.c b/src/msgb.c
index 940135f..4edbdf3 100644
--- a/src/msgb.c
+++ b/src/msgb.c
@@ -511,7 +511,7 @@
*/
const char *msgb_hexdump(const struct msgb *msg)
{
- static char buf[4100];
+ static __thread char buf[4100];
return msgb_hexdump_buf(buf, sizeof(buf), msg);
}
diff --git a/src/sim/core.c b/src/sim/core.c
index 63b3000..b93633c 100644
--- a/src/sim/core.c
+++ b/src/sim/core.c
@@ -300,8 +300,7 @@
char *osim_print_sw(const struct osim_card_hdl *ch, uint16_t sw_in)
{
- /* FIXME: do we want to mark this as __thread? */
- static char sw_print_buf[256];
+ static __thread char sw_print_buf[256];
return osim_print_sw_buf(sw_print_buf, sizeof(sw_print_buf), ch, sw_in);
}
diff --git a/src/socket.c b/src/socket.c
index 37976e2..1e303bf 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -824,7 +824,7 @@
*/
const char *osmo_sock_get_name2(int fd)
{
- static char str[OSMO_SOCK_NAME_MAXLEN];
+ static __thread char str[OSMO_SOCK_NAME_MAXLEN];
osmo_sock_get_name_buf(str, sizeof(str), fd);
return str;
}
diff --git a/src/utils.c b/src/utils.c
index 59dc816..c38f530 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -41,7 +41,7 @@
*
* \file utils.c */
-static char namebuf[255];
+static __thread char namebuf[255];
/*! get human-readable string for given value
* \param[in] vs Array of value_string tuples
@@ -219,7 +219,7 @@
return nibblepos >> 1;
}
-static char hexd_buff[4096];
+static __thread char hexd_buff[4096];
static const char hex_chars[] = "0123456789abcdef";
/*! Convert binary sequence to hexadecimal ASCII string.
@@ -492,7 +492,7 @@
* sizeof(uint64_t), only the least significant bytes of value are encoded. */
uint8_t *osmo_encode_big_endian(uint64_t value, size_t data_len)
{
- static uint8_t buf[sizeof(uint64_t)];
+ static __thread uint8_t buf[sizeof(uint64_t)];
OSMO_ASSERT(data_len <= ARRAY_SIZE(buf));
osmo_store64be_ext(value, buf, data_len);
return buf;
@@ -908,7 +908,7 @@
*/
const char *osmo_str_tolower(const char *src)
{
- static char buf[128];
+ static __thread char buf[128];
osmo_str_tolower_buf(buf, sizeof(buf), src);
return buf;
}
@@ -967,7 +967,7 @@
*/
const char *osmo_str_toupper(const char *src)
{
- static char buf[128];
+ static __thread char buf[128];
osmo_str_toupper_buf(buf, sizeof(buf), src);
return buf;
}