|
7 | 7 | #include <iostream>
|
8 | 8 | #include <map>
|
9 | 9 |
|
10 |
| -std::mutex g_db_mutex; |
| 10 | +/** |
| 11 | + * @brief Global mutex for thread synchronization |
| 12 | + * |
| 13 | + * Purpose of this mutex is to synchronize multiple threads like main thread, |
| 14 | + * counters and notifications as well as all operations which require multiple |
| 15 | + * Redis DB access. |
| 16 | + * |
| 17 | + * For example: query DB for next VID id number, and then put map RID and VID |
| 18 | + * to Redis. From syncd point of view this entire operation should be atomic |
| 19 | + * and no other thread should access DB or make assumption on previous |
| 20 | + * information until entire operation will finish. |
| 21 | + */ |
| 22 | +std::mutex g_mutex; |
11 | 23 |
|
12 | 24 | std::shared_ptr<swss::RedisClient> g_redisClient;
|
13 | 25 | std::shared_ptr<swss::ProducerTable> getResponse;
|
@@ -41,7 +53,7 @@ std::map<sai_object_id_t, std::shared_ptr<SaiSwitch>> switches;
|
41 | 53 | * could be vlan members, bridge ports etc.
|
42 | 54 | *
|
43 | 55 | * We need this list to later on not put them back to temp view mode when doing
|
44 |
| - * populate existing obejcts in apply view mode. |
| 56 | + * populate existing objects in apply view mode. |
45 | 57 | *
|
46 | 58 | * Object ids here a VIDs.
|
47 | 59 | */
|
@@ -327,8 +339,6 @@ sai_object_id_t translate_rid_to_vid(
|
327 | 339 | _In_ sai_object_id_t rid,
|
328 | 340 | _In_ sai_object_id_t switch_vid)
|
329 | 341 | {
|
330 |
| - std::lock_guard<std::mutex> lock(g_db_mutex); |
331 |
| - |
332 | 342 | SWSS_LOG_ENTER();
|
333 | 343 |
|
334 | 344 | /*
|
@@ -516,7 +526,6 @@ void translate_rid_to_vid_list(
|
516 | 526 | sai_object_id_t translate_vid_to_rid(
|
517 | 527 | _In_ sai_object_id_t vid)
|
518 | 528 | {
|
519 |
| - std::lock_guard<std::mutex> lock(g_db_mutex); |
520 | 529 |
|
521 | 530 | SWSS_LOG_ENTER();
|
522 | 531 |
|
@@ -679,7 +688,6 @@ void snoop_get_attr(
|
679 | 688 |
|
680 | 689 | SWSS_LOG_DEBUG("%s", key.c_str());
|
681 | 690 |
|
682 |
| - std::lock_guard<std::mutex> lock(g_db_mutex); |
683 | 691 |
|
684 | 692 | g_redisClient->hset(key, attr_id, attr_value);
|
685 | 693 | }
|
@@ -1208,7 +1216,6 @@ sai_status_t handle_generic(
|
1208 | 1216 | */
|
1209 | 1217 |
|
1210 | 1218 | {
|
1211 |
| - std::lock_guard<std::mutex> lock(g_db_mutex); |
1212 | 1219 |
|
1213 | 1220 | g_redisClient->hset(VIDTORID, str_vid, str_rid);
|
1214 | 1221 | g_redisClient->hset(RIDTOVID, str_rid, str_vid);
|
@@ -1246,7 +1253,6 @@ sai_status_t handle_generic(
|
1246 | 1253 | */
|
1247 | 1254 |
|
1248 | 1255 | {
|
1249 |
| - std::lock_guard<std::mutex> lock(g_db_mutex); |
1250 | 1256 |
|
1251 | 1257 | g_redisClient->hdel(VIDTORID, str_vid);
|
1252 | 1258 | g_redisClient->hdel(RIDTOVID, str_rid);
|
@@ -1411,7 +1417,6 @@ void clearTempView()
|
1411 | 1417 | * We need to expose api to execute user lua script not only predefined.
|
1412 | 1418 | */
|
1413 | 1419 |
|
1414 |
| - std::lock_guard<std::mutex> lock(g_db_mutex); |
1415 | 1420 |
|
1416 | 1421 | for (const auto &key: g_redisClient->keys(pattern))
|
1417 | 1422 | {
|
@@ -2107,6 +2112,8 @@ sai_status_t processBulkEvent(
|
2107 | 2112 | sai_status_t processEvent(
|
2108 | 2113 | _In_ swss::ConsumerTable &consumer)
|
2109 | 2114 | {
|
| 2115 | + std::lock_guard<std::mutex> lock(g_mutex); |
| 2116 | + |
2110 | 2117 | SWSS_LOG_ENTER();
|
2111 | 2118 |
|
2112 | 2119 | swss::KeyOpFieldsValuesTuple kco;
|
@@ -2613,7 +2620,6 @@ bool handleRestartQuery(swss::NotificationConsumer &restartQuery)
|
2613 | 2620 |
|
2614 | 2621 | bool isVeryFirstRun()
|
2615 | 2622 | {
|
2616 |
| - std::lock_guard<std::mutex> lock(g_db_mutex); |
2617 | 2623 |
|
2618 | 2624 | SWSS_LOG_ENTER();
|
2619 | 2625 |
|
@@ -2761,6 +2767,8 @@ void performWarmRestart()
|
2761 | 2767 |
|
2762 | 2768 | void onSyncdStart(bool warmStart)
|
2763 | 2769 | {
|
| 2770 | + std::lock_guard<std::mutex> lock(g_mutex); |
| 2771 | + |
2764 | 2772 | /*
|
2765 | 2773 | * It may happen that after initialize we will receive some port
|
2766 | 2774 | * notifications with port'ids that are not in redis db yet, so after
|
@@ -2987,6 +2995,8 @@ int main(int argc, char **argv)
|
2987 | 2995 | startCountersThread(options.countersThreadIntervalInSeconds);
|
2988 | 2996 | }
|
2989 | 2997 |
|
| 2998 | + startNotificationsProcessingThread(); |
| 2999 | + |
2990 | 3000 | SWSS_LOG_NOTICE("syncd listening for events");
|
2991 | 3001 |
|
2992 | 3002 | swss::Select s;
|
@@ -3056,6 +3066,8 @@ int main(int argc, char **argv)
|
3056 | 3066 | SWSS_LOG_ERROR("failed to uninitialize api: %s", sai_serialize_status(status).c_str());
|
3057 | 3067 | }
|
3058 | 3068 |
|
| 3069 | + stopNotificationsProcessingThread(); |
| 3070 | + |
3059 | 3071 | SWSS_LOG_NOTICE("uninitialize finished");
|
3060 | 3072 |
|
3061 | 3073 | exit(EXIT_SUCCESS);
|
|
0 commit comments