fatal-signal: Make thread-safe.
[sliver-openvswitch.git] / lib / jsonrpc.c
index 56b4cce..6c482c2 100644 (file)
@@ -26,6 +26,7 @@
 #include "json.h"
 #include "list.h"
 #include "ofpbuf.h"
+#include "ovs-thread.h"
 #include "poll-loop.h"
 #include "reconnect.h"
 #include "stream.h"
@@ -41,6 +42,7 @@ struct jsonrpc {
 
     /* Input. */
     struct byteq input;
+    uint8_t input_buffer[512];
     struct json_parser *parser;
     struct jsonrpc_msg *received;
 
@@ -87,7 +89,7 @@ jsonrpc_open(struct stream *stream)
     rpc = xzalloc(sizeof *rpc);
     rpc->name = xstrdup(stream_get_name(stream));
     rpc->stream = stream;
-    byteq_init(&rpc->input);
+    byteq_init(&rpc->input, rpc->input_buffer, sizeof rpc->input_buffer);
     list_init(&rpc->output);
 
     return rpc;
@@ -330,7 +332,7 @@ jsonrpc_recv(struct jsonrpc *rpc, struct jsonrpc_msg **msgp)
                 jsonrpc_received(rpc);
                 if (rpc->status) {
                     const struct byteq *q = &rpc->input;
-                    if (q->head <= BYTEQ_SIZE) {
+                    if (q->head <= q->size) {
                         stream_report_content(q->buffer, q->head,
                                               STREAM_JSONRPC,
                                               THIS_MODULE, rpc->name);
@@ -513,8 +515,15 @@ jsonrpc_create(enum jsonrpc_msg_type type, const char *method,
 static struct json *
 jsonrpc_create_id(void)
 {
-    static unsigned int id;
-    return json_integer_create(id++);
+    static pthread_mutex_t mutex = PTHREAD_ADAPTIVE_MUTEX_INITIALIZER;
+    static unsigned int next_id;
+    unsigned int id;
+
+    xpthread_mutex_lock(&mutex);
+    id = next_id++;
+    xpthread_mutex_unlock(&mutex);
+
+    return json_integer_create(id);
 }
 
 struct jsonrpc_msg *