Client example: cleanup globals 05/276605/2
authorMichal Bloch <m.bloch@samsung.com>
Tue, 21 Jun 2022 14:54:59 +0000 (16:54 +0200)
committerMichal Bloch <m.bloch@samsung.com>
Wed, 22 Jun 2022 13:05:37 +0000 (15:05 +0200)
 * the loop variable can be local.

 * it is safe to initialize an atomic variable
   non-atomically as long as there's only 1 thread.

 * make global constants into constexpr vars,
   as opposed to regular const vars or macros.

Change-Id: I006831917517db7e9fe0aed4a78aefccae1f2c0b
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
clientExample/app/main.cpp

index 8d9ff9f..09132f9 100644 (file)
@@ -5,14 +5,14 @@
 #include <unistd.h>
 #include "sessiond.h"
 
-#define SESSION_UID 5001    // default Tizen's 'owner' uid
-const int firstUser = 1;    // N.B. Starting from 1, as user number 0 is reserved
-const int lastUser = 10;
-const int noOfUsers = lastUser - firstUser + 1;
+static constexpr int SESSION_UID = 5001; // default Tizen's 'owner' uid
+
+static constexpr int firstUser = 1;  // N.B. Starting from 1, as user number 0 is reserved
+static constexpr int lastUser = 10;
+static constexpr int noOfUsers = lastUser - firstUser + 1;
 
 GMutex mutex;
 volatile int callbackCount;
-GMainLoop *loop = nullptr;
 
 typedef struct {
        int callback_result;
@@ -32,7 +32,7 @@ typedef struct {
 // the array as 1-based, so we need to add 1 more element.
 test_user_data userD[noOfUsers + 1];
 
-int callback_pending_reference;
+int callback_pending_reference = 0;
 
 void test_subsession_switch_user_completion_callback(subsession_event_info info, void *cb_data)
 {
@@ -47,7 +47,7 @@ void test_subsession_switch_user_completion_callback(subsession_event_info info,
 
 gboolean callback_pending(gpointer data)
 {
-       GMainLoop *lp = (GMainLoop *)data;
+       GMainLoop *loop = (GMainLoop *)data;
 
        gboolean is_pending = g_main_context_pending(NULL);
        gint ctrl_value = g_atomic_int_get(&callback_pending_reference);
@@ -58,7 +58,7 @@ gboolean callback_pending(gpointer data)
        // N.B. There are `noOfUsers` add and remove operations. Each add/remove
        // increments the value of the `callback_pending_reference` variable.
        if (ctrl_value >= noOfUsers * 2)
-               g_main_loop_quit(lp);
+               g_main_loop_quit(loop);
 
        return TRUE;
 }
@@ -114,10 +114,9 @@ int switch_user_test(int user_id)
 
 int main(int argc, char *argv[])
 {
-       loop = g_main_loop_new(NULL, FALSE);
+       GMainLoop *loop = g_main_loop_new(NULL, FALSE);
        g_mutex_init(&mutex);
        std::thread g_main_loop_thread(g_main_loop_run, loop);
-       g_atomic_int_set(&callback_pending_reference, 0);
 
        ///===================================///
        printf("Test program start\nCreating test users...");