IOT-1476 Remove pthread from sampleserver_randompin
authorDavid Antler <david.a.antler@intel.com>
Wed, 26 Oct 2016 18:18:11 +0000 (11:18 -0700)
committerRandeep Singh <randeep.s@samsung.com>
Mon, 7 Nov 2016 04:49:36 +0000 (04:49 +0000)
- Using C++ threads as a cross-platform approach
- Added .gitattributes file to ensure *.dat files are binary

Change-Id: I602649d55ee5a8e24a2215d5f68ca5897c9cc6c7
Signed-off-by: David Antler <david.a.antler@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/13727
Reviewed-by: Kevin Kane <kkane@microsoft.com>
Reviewed-by: Alex Kelley <alexke@microsoft.com>
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Dan Mihai <Daniel.Mihai@microsoft.com>
Reviewed-by: Dave Thaler <dthaler@microsoft.com>
Reviewed-by: Randeep Singh <randeep.s@samsung.com>
.gitattributes [new file with mode: 0644]
resource/csdk/security/provisioning/sample/sampleserver_randompin.cpp

diff --git a/.gitattributes b/.gitattributes
new file mode 100644 (file)
index 0000000..6935b1e
--- /dev/null
@@ -0,0 +1,2 @@
+\r
+*.dat binary\r
index 8837e38..dc10ede 100644 (file)
@@ -27,8 +27,8 @@
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
-#ifdef HAVE_PTHREAD_H
-#include <pthread.h>
+#ifdef HAVE_TIME_H
+#include <time.h>
 #endif
 #include <signal.h>
 #include "ocstack.h"
@@ -48,9 +48,6 @@
 #define TAG "SAMPLE_RANDOMPIN"
 
 int gQuitFlag = 0;
-#ifdef _ENABLE_MULTIPLE_OWNER_
-static bool g_LoopFlag = true;
-#endif //_ENABLE_MULTIPLE_OWNER_
 
 /* Structure to represent a LED resource */
 typedef struct LEDRESOURCE{
@@ -146,14 +143,16 @@ const char *getResult(OCStackResult result) {
 }
 
 #ifdef _ENABLE_MULTIPLE_OWNER_
-static pthread_t oc_process_thread;
 
-static void* oc_process_loop(void* ptr)
-{
-    struct timespec timeout;
-    timeout.tv_sec  = 0;
-    timeout.tv_nsec = 100000000L;
+#include <assert.h>
+#include <thread>
+#include <chrono>
+
+static bool volatile g_LoopFlag;
+static std::thread* oc_process_thread;
 
+static void oc_process_loop()
+{
     while(g_LoopFlag)
     {
         if (OCProcess() != OC_STACK_OK)
@@ -162,20 +161,21 @@ static void* oc_process_loop(void* ptr)
             g_LoopFlag = false;
             break;
         }
-        nanosleep(&timeout, NULL);
+        std::this_thread::sleep_for(std::chrono::milliseconds(100));
     }
-    pthread_join(&oc_process_thread, NULL);
-    return NULL;
 }
 
 static void StartOCProcessThread()
 {
-    pthread_create(&oc_process_thread, NULL, oc_process_loop, NULL);
+    g_LoopFlag = true;
+    oc_process_thread = new std::thread(oc_process_loop);
 }
 
 static void StopOCProcessThread()
 {
+    assert(oc_process_thread->joinable() == true);
     g_LoopFlag = false;
+    oc_process_thread->join();
 }
 #endif //_ENABLE_MULTIPLE_OWNER_