[IOT-1558] Add notification consumer for windows. 29/15929/2
authorKIM JungYong <jyong2.kim@samsung.com>
Fri, 23 Dec 2016 04:26:52 +0000 (13:26 +0900)
committerUze Choi <uzchoi@samsung.com>
Tue, 27 Dec 2016 09:08:21 +0000 (09:08 +0000)
Building of notification consumer service is enabled for windows.
And consumer service sample is added for windows.

Improvement:https://jira.iotivity.org/browse/IOT-1558
Change-Id: I6226aade0d3c9683db203b188030ea0118cd0f1b
Signed-off-by: KIM JungYong <jyong2.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/15929
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
14 files changed:
service/SConscript
service/notification/SConscript
service/notification/examples/SConscript
service/notification/examples/windows/SConscript [new file with mode: 0644]
service/notification/examples/windows/notificationconsumer.c [new file with mode: 0644]
service/notification/src/common/NSConstants.h
service/notification/src/consumer/NSConsumerCommon.c
service/notification/src/consumer/NSConsumerCommunication.c
service/notification/src/consumer/NSConsumerInternalTaskController.c
service/notification/src/consumer/NSConsumerMemoryCache.c
service/notification/src/consumer/NSConsumerMemoryCache.h
service/notification/src/consumer/NSConsumerScheduler.c
service/notification/src/consumer/NSThread.c
service/notification/src/consumer/NSThread.h

index 409767c..87e2d7a 100755 (executable)
@@ -37,10 +37,6 @@ if target_os not in ['arduino','darwin', 'ios', 'windows']:
     if target_os in ['linux']:
         SConscript('scene-manager/SConscript')
 
-    # Build notification-service project
-    if target_os in ['linux','android','tizen']:
-        SConscript('notification/SConscript')
-
     # Build simulator module
     if target_os in ['linux'] and env.get('SIMULATOR', False):
         SConscript('simulator/SConscript')
@@ -52,3 +48,7 @@ if target_os not in ['arduino','darwin', 'ios', 'windows']:
 # Build EasySetup module
 if target_os in ['arduino', 'android', 'linux','tizen']:
     SConscript('easy-setup/SConscript')
+
+# Build notification-service project
+if target_os in ['linux', 'android', 'windows', 'tizen']:
+    SConscript('notification/SConscript')
index 9287117..2e13072 100755 (executable)
@@ -26,13 +26,11 @@ import platform
 Import('env')
 
 if env.get('RELEASE'):
-       env.AppendUnique(CCFLAGS = ['-Os'])
-       env.AppendUnique(CPPDEFINES = ['NDEBUG'])
-else:
-       env.AppendUnique(CCFLAGS = ['-g'])
+    env.AppendUnique(CCFLAGS = ['-Os'])
+    env.AppendUnique(CPPDEFINES = ['NDEBUG'])
 
 if env.get('LOGGING'):
-       env.AppendUnique(CPPDEFINES = ['TB_LOG'])
+    env.AppendUnique(CPPDEFINES = ['TB_LOG'])
 
 lib_env = env.Clone()
 SConscript(env.get('SRC_DIR') + '/service/third_party_libs.scons', 'lib_env')
@@ -46,46 +44,49 @@ notification_env.AppendUnique(CPPPATH = ['include'])
 notification_env.AppendUnique(CPPPATH = ['src/common'])
 notification_env.AppendUnique(CPPPATH = ['src/provider'])
 notification_env.AppendUnique(CPPPATH = ['src/consumer'])
+notification_env.AppendUnique(CPPPATH = ['../../resource/c_common/octhread/include'])
 notification_env.AppendUnique(CPPPATH = ['../../resource/csdk/stack/include'])
 notification_env.AppendUnique(CPPPATH = ['../../resource/csdk/resource-directory/include'])
 notification_env.AppendUnique(CPPPATH = ['../../resource/csdk/connectivity/api'])
 
-notification_env.PrependUnique(LIBS = [
-       'octbstack',
-       'oc_logger',
-       'connectivity_abstraction',
-       'libcoap',
-       'resource_directory'
-       ])
-
-if target_os not in ['windows', 'winrt']:
-       notification_env.AppendUnique(CCFLAGS = ['-O2', '-g', '-Wall', '-fmessage-length=0'])
+notification_env.PrependUnique(LIBS = ['coap'])
+notification_env.AppendUnique(LIBS = ['connectivity_abstraction'])
+notification_env.AppendUnique(LIBS = ['oc_logger'])
+notification_env.AppendUnique(LIBS = ['octbstack'])
+if 'CLIENT' in notification_env.get('RD_MODE'):
+    notification_env.AppendUnique(LIBS = ['resource_directory'])
 
+if target_os not in ['windows', 'winrt', 'msys_nt']:
+    notification_env.AppendUnique(CCFLAGS = ['-O2', '-g', '-Wall', '-fmessage-length=0'])
+else:
+    notification_env.PrependUnique(LIBS = ['mswsock', 'ws2_32', 'iphlpapi', 'ole32', 'bcrypt', 'Crypt32'])
+    notification_env.AppendUnique(CPPDEFINES = ['SYSTEM_WINDOWS'])
+    
 if target_os not in ['darwin', 'ios', 'windows', 'winrt']:
-       notification_env.AppendUnique(LINKFLAGS = ['-Wl,--no-undefined'])
+    notification_env.AppendUnique(LINKFLAGS = ['-Wl,--no-undefined'])
 
 if target_os == 'linux':
-       notification_env.AppendUnique(LIBS = ['pthread'])
-       if not env.get('RELEASE'):
-               notification_env.PrependUnique(LIBS = ['gcov'])
-               notification_env.AppendUnique(CCFLAGS = ['--coverage'])
+    notification_env.AppendUnique(LIBS = ['pthread'])
+    if not env.get('RELEASE'):
+        notification_env.PrependUnique(LIBS = ['gcov'])
+        notification_env.AppendUnique(CCFLAGS = ['--coverage'])
 
 if target_os == 'android':
-       notification_env.AppendUnique(CCFLAGS = ['-frtti', '-fexceptions'])
-       notification_env.AppendUnique(LIBS = ['gnustl_shared','log'])
-
-       if not env.get('RELEASE'):
-               notification_env.AppendUnique(LIBS = ['log'])
+    notification_env.AppendUnique(CCFLAGS = ['-frtti', '-fexceptions'])
+    notification_env.AppendUnique(LIBS = ['gnustl_shared','log'])
+    if not env.get('RELEASE'):
+        notification_env.AppendUnique(LIBS = ['log'])
 
 if target_os == 'tizen':
-       notification_env.AppendUnique(CPPDEFINES = ['__TIZEN__'])
-       # notification_env.ParseConfig('pkg-config --cflags --libs sqlite3')
+    notification_env.AppendUnique(CPPDEFINES = ['__TIZEN__'])
 
 if env.get('WITH_CLOUD') == True:
-       notification_env.AppendUnique(CPPDEFINES = ['WITH_CLOUD'])
+    notification_env.AppendUnique(CPPDEFINES = ['WITH_CLOUD'])
 
 if env.get('SECURED') == '1':
-       notification_env.AppendUnique(CPPDEFINES = ['SECURED'])
+    notification_env.AppendUnique(CPPDEFINES = ['SECURED'])
+    if target_os in ['windows', 'winrt', 'msys_nt']:
+        notification_env.AppendUnique(LIBS = ['advapi32'])
        
 with_mq = env.get('WITH_MQ')
 if 'SUB' in with_mq:
@@ -100,7 +101,6 @@ if 'BROKER' in with_mq:
     notification_env.AppendUnique(CPPDEFINES = ['MQ_BROKER', 'WITH_MQ'])
     print "MQ Broker support"
 
-
 ######################################################################
 # Source files and Targets
 ######################################################################
@@ -108,43 +108,43 @@ notification_provider_env = notification_env.Clone()
 notification_consumer_env = notification_env.Clone()
 
 if target_os == 'android':
-       notification_provider_env.AppendUnique(LINKFLAGS = ['-Wl,-soname,libnotification_provider.so'])
-if target_os == 'android':
-       notification_consumer_env.AppendUnique(LINKFLAGS = ['-Wl,-soname,libnotification_consumer.so'])
+    notification_provider_env.AppendUnique(LINKFLAGS = ['-Wl,-soname,libnotification_provider.so'])
+    notification_consumer_env.AppendUnique(LINKFLAGS = ['-Wl,-soname,libnotification_consumer.so'])
 
-notification_provider_src = [
-       env.Glob('src/provider/*.c'), env.Glob('src/common/*.c')]
 notification_consumer_src = [
-       env.Glob('src/consumer/*.c'), env.Glob('src/common/*.c')]
-
-
-providersdk = notification_provider_env.SharedLibrary('notification_provider', notification_provider_src)
-notification_provider_env.InstallTarget(providersdk, 'libnotification_provider')
-notification_provider_env.UserInstallTargetLib(providersdk, 'libnotification_provider')
-
-consumersdk = notification_consumer_env.SharedLibrary('notification_consumer', notification_consumer_src)
-notification_consumer_env.InstallTarget(consumersdk, 'libnotification_consumer')
-notification_consumer_env.UserInstallTargetLib(consumersdk, 'libnotification_consumer')
-
-providersdk = notification_provider_env.StaticLibrary('notification_provider', notification_provider_src)
-notification_provider_env.InstallTarget(providersdk, 'libnotification_provider')
-notification_provider_env.UserInstallTargetLib(providersdk, 'libnotification_provider')
+    env.Glob('src/consumer/*.c'), env.Glob('src/common/*.c')]
+notification_consumer_env.UserInstallTargetHeader('include/NSConsumerInterface.h',\
+    'service/notification', 'NSConsumerInterface.h')
+
+if target_os not in ['windows', 'winrt', 'msys_nt']:
+    notification_provider_src = [
+        env.Glob('src/provider/*.c'), env.Glob('src/common/*.c')]
+    notification_provider_env.UserInstallTargetHeader('include/NSProviderInterface.h',\
+        'service/notification', 'NSProviderInterface.h')
+        
+    providersdk = notification_provider_env.SharedLibrary('notification_provider', notification_provider_src)
+    notification_provider_env.InstallTarget(providersdk, 'libnotification_provider')
+    notification_provider_env.UserInstallTargetLib(providersdk, 'libnotification_provider')
+    
+    providersdk = notification_provider_env.StaticLibrary('notification_provider', notification_provider_src)
+    notification_provider_env.InstallTarget(providersdk, 'libnotification_provider')
+    notification_provider_env.UserInstallTargetLib(providersdk, 'libnotification_provider')
+
+    consumersdk = notification_consumer_env.SharedLibrary('notification_consumer', notification_consumer_src)
+    notification_consumer_env.InstallTarget(consumersdk, 'libnotification_consumer')
+    notification_consumer_env.UserInstallTargetLib(consumersdk, 'libnotification_consumer')
 
 consumersdk = notification_consumer_env.StaticLibrary('notification_consumer', notification_consumer_src)
 notification_consumer_env.InstallTarget(consumersdk, 'libnotification_consumer')
 notification_consumer_env.UserInstallTargetLib(consumersdk, 'libnotification_consumer')
 
-notification_provider_env.UserInstallTargetHeader('include/NSProviderInterface.h',\
-       'service/notification', 'NSProviderInterface.h')
-notification_consumer_env.UserInstallTargetHeader('include/NSConsumerInterface.h',\
-       'service/notification', 'NSConsumerInterface.h')
-
 # Go to build Unit test
 if target_os == 'linux':
     SConscript('unittest/SConscript')
 
 # Go to build c++ wrapper
-SConscript('cpp-wrapper/SConscript')
+if target_os not in ['windows', 'msys_nt']:
+    SConscript('cpp-wrapper/SConscript')
 
 
 if target_os == 'android':
index c51a751..5da73de 100644 (file)
@@ -5,6 +5,8 @@ Import('env')
 
 target_os = env.get('TARGET_OS')
 if target_os == 'linux':
-       SConscript('linux/SConscript')
+    SConscript('linux/SConscript')
 elif target_os == 'android':
-       SConscript('android/SConscript')
\ No newline at end of file
+    SConscript('android/SConscript')
+elif target_os == 'windows':
+    SConscript('windows/SConscript')
\ No newline at end of file
diff --git a/service/notification/examples/windows/SConscript b/service/notification/examples/windows/SConscript
new file mode 100644 (file)
index 0000000..ee9ebfb
--- /dev/null
@@ -0,0 +1,54 @@
+##
+# Notification build script
+##
+
+Import('env')
+
+lib_env = env.Clone()
+SConscript(env.get('SRC_DIR') + '/service/third_party_libs.scons', 'lib_env')
+notification_sample_env = lib_env.Clone()
+
+target_os = env.get('TARGET_OS')
+rd_mode = env.get('RD_MODE')
+######################################################################
+# Build flags
+######################################################################
+notification_sample_env.AppendUnique(CPPPATH = ['../../include'])
+notification_sample_env.AppendUnique(CPPPATH = ['../../src/common'])
+notification_sample_env.AppendUnique(CPPPATH = ['../../src/provider'])
+notification_sample_env.AppendUnique(CPPPATH = ['../../../../resource/csdk/stack/include'])
+notification_sample_env.AppendUnique(CPPPATH = ['../../../../resource/csdk/resource-directory/include'])
+notification_sample_env.AppendUnique(CPPPATH = ['../../../../resource/csdk/connectivity/api'])
+
+if lib_env.get('SECURED') == '1':
+    notification_sample_env.AppendUnique(LIBS = ['mbedtls','mbedx509','mbedcrypto', 'tinydtls'])
+
+notification_sample_env.PrependUnique(LIBS = [
+       'octbstack_static',
+       'oc_logger',
+       'connectivity_abstraction',
+       'coap',
+       'ocsrm'
+       ])
+
+notification_sample_env.PrependUnique(LIBS = ['mswsock', 'ws2_32', 'iphlpapi', 'ole32', 'bcrypt', 'Crypt32'])
+
+notification_sample_consumer_src = 'notificationconsumer.c'
+
+if env.get('WITH_TCP') == True:
+       notification_sample_env.AppendUnique(CPPDEFINES = ['WITH_TCP'])
+if env.get('SECURED') == '1':
+       notification_sample_env.AppendUnique(LIBS = ['mbedtls', 'mbedx509', 'mbedcrypto'])
+
+if 'CLIENT' in rd_mode or 'SERVER' in rd_mode:
+       notification_sample_env.AppendUnique(LIBS = ['resource_directory'])
+
+####################################################################
+# Source files and Targets
+######################################################################
+
+notification_sample_consumer_env = notification_sample_env.Clone()
+notification_sample_consumer_env.AppendUnique(LIBS = 'notification_consumer')
+notificationconsumer = notification_sample_consumer_env.Program('notificationconsumer', notification_sample_consumer_src)
+i_notificationconsumer = notification_sample_consumer_env.Install(env.get('BUILD_DIR'), notificationconsumer)
+
diff --git a/service/notification/examples/windows/notificationconsumer.c b/service/notification/examples/windows/notificationconsumer.c
new file mode 100644 (file)
index 0000000..0c34f3b
--- /dev/null
@@ -0,0 +1,307 @@
+//******************************************************************
+//
+// Copyright 2016 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#include <stdio.h>
+#include <string.h>
+#include "octhread.h"
+
+#include "ocstack.h"
+#include "NSCommon.h"
+#include "NSConsumerInterface.h"
+
+#ifdef SYSTEM_WINDOWS
+#include <unistd.h>
+#else
+#include "win_sleep.h"
+#endif
+
+#ifdef WITH_CLOUD
+#include "NSConstants.h"
+#include "oic_malloc.h"
+#include "cloud_connector.h"
+
+#define CLOUD_CONTEXT_VALUE 0x99
+
+char CLOUD_ADDRESS[100];
+char CLOUD_AUTH_PROVIDER[100];
+char CLOUD_AUTH_CODE[100];
+char CLOUD_UID[100];
+char CLOUD_ACCESS_TOKEN[100];
+#endif
+
+
+NSProvider * g_provider = NULL;
+NSTopicLL * g_topicLL = NULL;
+
+FILE* server_fopen(const char *path, const char *mode)
+{
+    (void)path;
+    return fopen("oic_ns_provider_db.dat", mode);
+}
+
+void printProviderTopicList(NSTopicLL * topics)
+{
+    printf("printProviderTopicList\n");
+    if (topics)
+    {
+        NSTopicLL * iter = topics;
+        while (iter)
+        {
+            printf("Topic Name: %s\t Topic State: %d\n", iter->topicName, iter->state);
+            iter = iter->next;
+        }
+    }
+}
+
+void onProviderChanged(NSProvider * provider, NSProviderState response)
+{
+    printf("Provider changed: %d\n", response);
+    printf("subscribed provider Id : %s\n", provider->providerId);
+
+    if (response == NS_DISCOVERED)
+    {
+        printf("notification resource discovered\n");
+        printf("subscribe result %d\n", NSSubscribe(provider->providerId));
+        printf("startSubscribing\n");
+    }
+
+    else if (response == NS_TOPIC)
+    {
+        printf ("Provider Topic Updated\n");
+        g_topicLL = NSConsumerGetTopicList(provider->providerId);
+        printProviderTopicList(g_topicLL);
+        g_provider = provider;
+    }
+}
+
+void onNotificationPosted(NSMessage * notification)
+{
+    printf("id : %lld\n", (long long int)notification->messageId);
+    printf("title : %s\n", notification->title);
+    printf("content : %s\n", notification->contentText);
+    printf("source : %s\n", notification->sourceName);
+    if (notification->topic && strlen(notification->topic) > 0)
+    {
+        printf("topic : %s\n", notification->topic);
+    }
+    NSConsumerSendSyncInfo(notification->providerId, notification->messageId, NS_SYNC_READ);
+}
+
+void onNotificationSync(NSSyncInfo * sync)
+{
+    printf("Sync ID : %lld\n", (long long int)sync->messageId);
+    printf("Sync STATE : %d\n", sync->state);
+}
+
+void* OCProcessThread(void * ptr)
+{
+    (void) ptr;
+
+    while (true)
+    {
+        if(OCProcess() != OC_STACK_OK)
+        {
+            OCStop();
+            break;
+        }
+    }
+
+    return NULL;
+}
+
+void input(char * buffer)
+{
+    char ch;
+    int i = 0;
+
+    while( (ch = getchar()) != '\n' && i < 100)
+        buffer[i++] = ch;
+
+    buffer[i] = '\0';
+}
+
+int main(void)
+{
+    bool isExit = false;
+    oc_thread OCThread = NULL;
+
+    printf("start Iotivity\n");
+
+    // open oic_db
+    static OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink};
+    OCRegisterPersistentStorageHandler(&ps);
+
+    if (OCInit1(OC_CLIENT_SERVER, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
+    {
+        printf("OCInit fail\n");
+        return 0;
+    }
+
+    NSConsumerConfig cfg;
+    cfg.changedCb = onProviderChanged;
+    cfg.messageCb = onNotificationPosted;
+    cfg.syncInfoCb = onNotificationSync;
+
+    OCThreadResult_t ret = oc_thread_new(&OCThread, OCProcessThread, NULL);
+    printf("create thread result : %d\n", ret);
+
+    printf("start notification consumer service\n");
+    while (!isExit)
+    {
+        int num = 0;
+        char dummy = '\0';
+
+        printf("1. Start Consumer\n");
+        printf("2. Stop Consumer\n");
+        printf("3. Get Topics\n");
+        printf("4. Select Topics\n");
+        printf("5. Cancel select Topics\n");
+        printf("0. Exit\n");
+#ifdef WITH_CLOUD
+        printf("21. Enable Remote Service (after login)\n");
+        printf("31. Cloud Signup\n");
+        printf("32. Cloud Login\n");
+        printf("33. Cloud Logout\n");
+#endif
+
+        printf("Input: ");
+
+        if(scanf("%d", &num) == EOF)
+        {
+            printf("Fail to input num\n");
+        }
+        fflush(stdin);
+
+        if(scanf("%c", &dummy) == EOF)
+        {
+            printf("Fail to input dummy\n");
+        }
+        fflush(stdin);
+
+        switch (num)
+        {
+            case 1:
+                printf("1. Start Consumer\n");
+                NSStartConsumer(cfg);
+                break;
+            case 2:
+                printf("2. Stop Consumer");
+                NSStopConsumer();
+                break;
+            case 3:
+                printf("3. Get Topics\n");
+                if(g_provider)
+                {
+                    g_topicLL = NSConsumerGetTopicList(g_provider->providerId);
+                    printProviderTopicList(g_topicLL);
+                }
+                break;
+            case 4:
+                printf("4. Select Topics\n");
+
+                if (g_provider && g_topicLL)
+                {
+                    NSTopicLL * iter = g_topicLL;
+                    int i = 0;
+                    while (iter)
+                    {
+                        iter->state = (i++)%2;
+                        iter = iter->next;
+                    }
+                    NSResult ret = NSConsumerUpdateTopicList(g_provider->providerId, g_topicLL);
+                    if (ret == NS_OK)
+                    {
+                        printProviderTopicList(g_topicLL);
+                    }
+                    else
+                    {
+                        printf("Update fail\n");
+                    }
+                }
+                break;
+            case 5:
+                printf("5. Cancel select Topics\n");
+                NSTopicLL * iter = g_topicLL;
+                while (iter)
+                {
+                    iter->state = NS_TOPIC_UNSUBSCRIBED;
+                    iter = iter->next;
+                }
+
+                NSResult ret = NSConsumerUpdateTopicList(g_provider->providerId, g_topicLL);
+                if (ret != NS_OK)
+                {
+                    printf("Cancel select topic fail\n");
+                }
+                break;
+            case 0:
+                printf("0. Exit");
+                isExit = true;
+                break;
+#ifdef WITH_CLOUD
+            case 21:
+                printf("Enable Remote Service");
+                if(!IsCloudLoggedin())
+                {
+                    printf("Cloud Login required");
+                    break;
+                }
+                NSConsumerEnableRemoteService(CLOUD_ADDRESS);
+                break;
+            case 31:
+                printf("Remote Server Address: ");
+                input(CLOUD_ADDRESS);
+
+                printf("Auth Provider(eg. github): ");
+                input(CLOUD_AUTH_PROVIDER);
+
+                printf("Auth Code: ");
+                input(CLOUD_AUTH_CODE);
+
+                OCCloudSignup(CLOUD_ADDRESS, OCGetServerInstanceIDString(),
+                    CLOUD_AUTH_PROVIDER, CLOUD_AUTH_CODE, CloudSignupCallback);
+                printf("OCCloudSignup requested");
+                break;
+            case 32:
+                printf("Remote Server Address: ");
+                input(CLOUD_ADDRESS);
+
+                printf("UID: ");
+                input(CLOUD_UID);
+
+                printf("ACCESS_TOKEN: ");
+                input(CLOUD_ACCESS_TOKEN);
+
+                OCCloudLogin(CLOUD_ADDRESS, CLOUD_UID, OCGetServerInstanceIDString(),
+                    CLOUD_ACCESS_TOKEN, CloudLoginoutCallback);
+                printf("OCCloudLogin requested");
+                break;
+            case 33:
+                OCCloudLogout(CLOUD_ADDRESS, CloudLoginoutCallback);
+                printf("OCCloudLogin requested");
+                break;
+#endif
+            default:
+                break;
+        }
+    }
+    return 0;
+}
+
index 2eda9c6..0175234 100644 (file)
         { \
             NS_LOG_V(ERROR, "%s : %s is null", __func__, #obj); \
             NS_LOG(ERROR, "execute deletion"); \
-            (func); \
+            func; \
             return; \
         } \
     }
         { \
             NS_LOG_V(ERROR, "%s : %s is null", __func__, #obj); \
             NS_LOG(ERROR, "execute deletion"); \
-            (func); \
+            func; \
             return (retVal); \
         } \
     }
         if ( _ret != true) \
         { \
             NS_LOG_V(ERROR, "%s : %s is not OC_STACK_OK : %d", __func__, #obj, _ret); \
-            (func); \
+            func; \
             return (retVal); \
         } \
     }
index 4a2c69a..0618302 100644 (file)
 #include "oic_string.h"
 #include "ocpayload.h"
 
-#include <pthread.h>
+#include "octhread.h"
 
 static bool NSIsExtraValue(const char * name);
 static void NSCopyPayloadValueArray(OCRepPayloadValue* dest, OCRepPayloadValue* source);
 static OCRepPayloadValue * NSCopyPayloadValue(OCRepPayloadValue * value);
 
-pthread_mutex_t ** NSGetStackMutex()
+oc_mutex * NSGetStackMutex()
 {
-    static pthread_mutex_t * g_stackMutext = NULL;
+    static oc_mutex g_stackMutext = NULL;
     if (g_stackMutext == NULL)
     {
-        g_stackMutext = (pthread_mutex_t *)OICMalloc(sizeof(pthread_mutex_t));
+        g_stackMutext = oc_mutex_new();
         NS_VERIFY_NOT_NULL(g_stackMutext, NULL);
-        pthread_mutex_init(g_stackMutext, NULL);
     }
 
     return & g_stackMutext;
@@ -104,8 +103,7 @@ void NSSetIsStartedConsumer(bool setValue)
 
     if (setValue == false)
     {
-        pthread_mutex_destroy(*NSGetStackMutex());
-        NSOICFree(*NSGetStackMutex());
+        oc_mutex_free(*NSGetStackMutex());
         *NSGetStackMutex() = NULL;
 
         NSOICFree(*NSGetConsumerId());
@@ -791,8 +789,7 @@ OCStackResult NSInvokeRequest(OCDoHandle * handle,
         void * callbackFunc, void * callbackData,
         OCClientContextDeleter cd, OCConnectivityType type)
 {
-    int mutexRet = pthread_mutex_lock(*(NSGetStackMutex()));
-    NS_VERIFY_NOT_NULL(mutexRet != 0 ? NULL : (void *)1, OC_STACK_ERROR);
+    oc_mutex_lock(*NSGetStackMutex());
 
     OCCallbackData cbdata = { NULL, NULL, NULL };
 
@@ -803,8 +800,7 @@ OCStackResult NSInvokeRequest(OCDoHandle * handle,
     OCStackResult ret = OCDoResource(handle, method, queryUrl, addr,
                                      payload, type, NS_QOS, &cbdata, NULL, 0);
 
-    mutexRet = pthread_mutex_unlock(*(NSGetStackMutex()));
-    NS_VERIFY_NOT_NULL(mutexRet != 0 ? NULL : (void *)1, OC_STACK_ERROR);
+    oc_mutex_unlock(*NSGetStackMutex());
 
     return ret;
 }
index 11e4182..1104b0d 100644 (file)
@@ -224,7 +224,7 @@ NSSyncInfo * NSGetSyncInfoc(OCClientResponse * clientResponse)
     OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
 
     NS_LOG(DEBUG, "get msg id");
-    uint64_t id = NULL;
+    uint64_t id = 0;
     bool getResult = OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_MESSAGE_ID, (int64_t *)&id);
     NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
 
index 3156979..a76614a 100644 (file)
@@ -25,6 +25,7 @@
 
 #include "oic_malloc.h"
 #include "oic_string.h"
+#include "octhread.h"
 
 #define NS_RESERVED_MESSAGEID 10
 
@@ -45,7 +46,7 @@ typedef struct
 } NSMessageStateList;
 
 // Mutex of MessageState storage
-pthread_mutex_t ** NSGetMessageListMutex();
+oc_mutex * NSGetMessageListMutex();
 void NSLockMessageListMutex();
 void NSUnlockMessageListMutex();
 
@@ -79,8 +80,8 @@ void NSDestroyInternalCachedList()
     NSSetProviderCacheList(NULL);
 
     NSDestroyMessageStateList();
-    pthread_mutex_destroy(*NSGetMessageListMutex());
-    NSOICFree(*NSGetMessageListMutex());
+    oc_mutex_free(*NSGetMessageListMutex());
+    *NSGetMessageListMutex() = NULL;
 }
 
 NSProvider_internal * NSProviderCacheFind(const char * providerId)
@@ -304,8 +305,8 @@ void NSConsumerHandleSubscribeSucceed(NSProvider_internal * provider)
     NSCacheElement * cacheElement = NSConsumerStorageRead(ProviderCache, provider->providerId);
     NS_VERIFY_NOT_NULL_V(cacheElement);
 
-    pthread_mutex_t * mutex = NSGetCacheMutex();
-    pthread_mutex_lock(mutex);
+    oc_mutex * mutex = NSGetCacheMutex();
+    oc_mutex_lock(*mutex);
 
     NS_VERIFY_NOT_NULL_V(cacheElement);
     NSProvider_internal * prov = (NSProvider_internal *)cacheElement->data;
@@ -316,7 +317,7 @@ void NSConsumerHandleSubscribeSucceed(NSProvider_internal * provider)
         infos = infos->next;
     }
 
-    pthread_mutex_unlock(mutex);
+    oc_mutex_unlock(*mutex);
 }
 
 void NSConsumerHandleRecvProviderChanged(NSMessage * msg)
@@ -330,14 +331,14 @@ void NSConsumerHandleRecvProviderChanged(NSMessage * msg)
     NSCacheElement * cacheElement = NSConsumerStorageRead(ProviderCache, msg->providerId);
     NS_VERIFY_NOT_NULL_V(cacheElement);
 
-    pthread_mutex_t * mutex = NSGetCacheMutex();
-    pthread_mutex_lock(mutex);
-    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(cacheElement, pthread_mutex_unlock(mutex));
+    oc_mutex * mutex = NSGetCacheMutex();
+    oc_mutex_lock(*mutex);
+    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(cacheElement, oc_mutex_unlock(*mutex));
     NSProvider_internal * provider = (NSProvider_internal *) cacheElement->data;
     if (provider->state == (NSProviderState) msg->messageId)
     {
         NS_LOG_V(DEBUG, "Already receive message(ALLOW/DENY) : %d", (int) msg->messageId);
-        pthread_mutex_unlock(mutex);
+        oc_mutex_unlock(*mutex);
         return;
     }
 
@@ -348,7 +349,7 @@ void NSConsumerHandleRecvProviderChanged(NSMessage * msg)
 
     NSProvider * prov = NSCopyProvider(provider);
 
-    pthread_mutex_unlock(mutex);
+    oc_mutex_unlock(*mutex);
     NSProviderChanged(prov, (NSProviderState) msg->messageId);
     NSRemoveProvider(prov);
 }
@@ -522,16 +523,13 @@ void NSConsumerInternalTaskProcessing(NSTask * task)
     NSOICFree(task);
 }
 
-// implements of MessageState function
-pthread_mutex_t ** NSGetMessageListMutex()
+oc_mutex * NSGetMessageListMutex()
 {
-    static pthread_mutex_t * g_mutex = NULL;
+    static oc_mutex g_mutex = NULL;
     if (g_mutex == NULL)
     {
-        g_mutex = (pthread_mutex_t *) OICMalloc(sizeof(pthread_mutex_t));
+        g_mutex = oc_mutex_new();
         NS_VERIFY_NOT_NULL(g_mutex, NULL);
-
-        pthread_mutex_init(g_mutex, NULL);
     }
     return & g_mutex;
 }
@@ -539,13 +537,13 @@ pthread_mutex_t ** NSGetMessageListMutex()
 void NSLockMessageListMutex()
 {
     NS_LOG_V(DEBUG, "%s", __func__);
-    pthread_mutex_lock(*NSGetMessageListMutex());
+    oc_mutex_lock(*NSGetMessageListMutex());
 }
 
 void NSUnlockMessageListMutex()
 {
     NS_LOG_V(DEBUG, "%s", __func__);
-    pthread_mutex_unlock(*NSGetMessageListMutex());
+    oc_mutex_unlock(*NSGetMessageListMutex());
 }
 
 NSMessageStateList ** NSGetMessageStateListAddr()
@@ -711,9 +709,7 @@ void NSDestroyMessageStateList()
 
     NSUnlockMessageListMutex();
 
-    pthread_mutex_t * mu = *NSGetMessageListMutex();
-    pthread_mutex_destroy(mu);
-    NSOICFree(mu);
+    oc_mutex_free(*NSGetMessageListMutex());
     *NSGetMessageListMutex() = NULL;
 
     NSMessageStateList * list = NSGetMessageStateList();
index a5a63e5..96d0ac4 100644 (file)
 #include "NSConsumerMemoryCache.h"
 #include "oic_malloc.h"
 #include "oic_string.h"
+#include "octhread.h"
 
-pthread_mutex_t * NSGetCacheMutex()
+oc_mutex * NSGetCacheMutex()
 {
-    static pthread_mutex_t * g_NSCacheMutex = NULL;
+    static oc_mutex g_NSCacheMutex = NULL;
     if (g_NSCacheMutex == NULL)
     {
-        g_NSCacheMutex = (pthread_mutex_t *) OICMalloc(sizeof(pthread_mutex_t));
+        g_NSCacheMutex = oc_mutex_new();
         NS_VERIFY_NOT_NULL(g_NSCacheMutex, NULL);
-
-        pthread_mutex_init(g_NSCacheMutex, NULL);
     }
-    return g_NSCacheMutex;
+    return g_NSCacheMutex;
 }
 
 NSCacheList * NSConsumerStorageCreate()
 {
-    pthread_mutex_t * mutex = NSGetCacheMutex();
-    pthread_mutex_lock(mutex);
+    oc_mutex * mutex = NSGetCacheMutex();
+    oc_mutex_lock(*mutex);
 
     NSCacheList * newList = (NSCacheList *) OICMalloc(sizeof(NSCacheList));
-    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(newList, NULL, pthread_mutex_unlock(mutex));
+    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(newList, NULL, oc_mutex_unlock(*mutex));
 
     newList->head = NULL;
     newList->tail = NULL;
 
-    pthread_mutex_unlock(mutex);
+    oc_mutex_unlock(*mutex);
 
     return newList;
 }
@@ -56,8 +55,8 @@ NSCacheElement * NSConsumerStorageRead(NSCacheList * list, const char * findId)
     NS_VERIFY_NOT_NULL(list, NULL);
     NS_VERIFY_NOT_NULL(findId, NULL);
 
-    pthread_mutex_t * mutex = NSGetCacheMutex();
-    pthread_mutex_lock(mutex);
+    oc_mutex * mutex = NSGetCacheMutex();
+    oc_mutex_lock(*mutex);
 
     NSCacheElement * iter = list->head;
     NSCacheType type = list->cacheType;
@@ -66,7 +65,7 @@ NSCacheElement * NSConsumerStorageRead(NSCacheList * list, const char * findId)
     {
         if (NSConsumerCompareIdCacheData(type, iter->data, findId))
         {
-            pthread_mutex_unlock(mutex);
+            oc_mutex_unlock(*mutex);
             return iter;
         }
 
@@ -74,7 +73,7 @@ NSCacheElement * NSConsumerStorageRead(NSCacheList * list, const char * findId)
     }
 
     NS_LOG (DEBUG, "No Cache Element");
-    pthread_mutex_unlock(mutex);
+    oc_mutex_unlock(*mutex);
     return NULL;
 }
 
@@ -85,8 +84,8 @@ NSCacheElement * NSGetProviderFromAddr(NSCacheList * list, const char * addr, ui
     NS_VERIFY_NOT_NULL(
             (list->cacheType != NS_CONSUMER_CACHE_PROVIDER) ? NULL : (void *) 1, NULL);
 
-    pthread_mutex_t * mutex = NSGetCacheMutex();
-    pthread_mutex_lock(mutex);
+    oc_mutex * mutex = NSGetCacheMutex();
+    oc_mutex_lock(*mutex);
 
     NSCacheElement * iter = list->head;
 
@@ -101,7 +100,7 @@ NSCacheElement * NSGetProviderFromAddr(NSCacheList * list, const char * addr, ui
 
             if (!strcmp(conAddr, addr) && conPort == port)
             {
-                pthread_mutex_unlock(mutex);
+                oc_mutex_unlock(*mutex);
                 return iter;
             }
             connection = connection->next;
@@ -111,7 +110,7 @@ NSCacheElement * NSGetProviderFromAddr(NSCacheList * list, const char * addr, ui
     }
 
     NS_LOG (DEBUG, "No Cache Element");
-    pthread_mutex_unlock(mutex);
+    oc_mutex_unlock(*mutex);
     return NULL;
 }
 
@@ -140,12 +139,12 @@ NSResult NSConsumerStorageDelete(NSCacheList * list, const char * delId)
 
     NSCacheType type = list->cacheType;
 
-    pthread_mutex_t * mutex = NSGetCacheMutex();
-    pthread_mutex_lock(mutex);
+    oc_mutex * mutex = NSGetCacheMutex();
+    oc_mutex_lock(*mutex);
 
     NSCacheElement * prev = list->head;
     NSCacheElement * del = list->head;
-    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(del, NS_ERROR, pthread_mutex_unlock(mutex));
+    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(del, NS_ERROR, oc_mutex_unlock(*mutex));
 
     if (NSConsumerCompareIdCacheData(type, del->data, delId))
     {
@@ -163,7 +162,7 @@ NSResult NSConsumerStorageDelete(NSCacheList * list, const char * delId)
                 NSRemoveProvider_internal((void *) del->data);
             }
             NSOICFree(del);
-            pthread_mutex_unlock(mutex);
+            oc_mutex_unlock(*mutex);
 
             return NS_OK;
         }
@@ -186,7 +185,7 @@ NSResult NSConsumerStorageDelete(NSCacheList * list, const char * delId)
                 NSRemoveProvider_internal((NSProvider_internal *) del->data);
             }
             NSOICFree(del);
-            pthread_mutex_unlock(mutex);
+            oc_mutex_unlock(*mutex);
 
             return NS_OK;
         }
@@ -194,7 +193,7 @@ NSResult NSConsumerStorageDelete(NSCacheList * list, const char * delId)
         prev = del;
         del = del->next;
     }
-    pthread_mutex_unlock(mutex);
+    oc_mutex_unlock(*mutex);
     return NS_OK;
 }
 
@@ -203,13 +202,13 @@ NSResult NSConsumerCacheWriteProvider(NSCacheList * list, NSCacheElement * newOb
     NS_VERIFY_NOT_NULL(list, NS_ERROR);
     NS_VERIFY_NOT_NULL(newObj, NS_ERROR);
 
-    pthread_mutex_t * mutex = NSGetCacheMutex();
+    oc_mutex * mutex = NSGetCacheMutex();
 
     NSProvider_internal * newProvObj = (NSProvider_internal *) newObj->data;
 
     NSCacheElement * it = NSConsumerStorageRead(list, newProvObj->providerId);
 
-    pthread_mutex_lock(mutex);
+    oc_mutex_lock(*mutex);
 
     if (it)
     {
@@ -234,13 +233,13 @@ NSResult NSConsumerCacheWriteProvider(NSCacheList * list, NSCacheElement * newOb
             provObj->topicLL = NSCopyTopicLL(newProvObj->topicLL);
         }
 
-        pthread_mutex_unlock(mutex);
+        oc_mutex_unlock(*mutex);
 
         return NS_OK;
     }
 
     NSCacheElement * obj = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));
-    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(obj, NS_ERROR, pthread_mutex_unlock(mutex));
+    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(obj, NS_ERROR, oc_mutex_unlock(*mutex));
 
     NS_LOG_V(DEBUG, "New Object address : %s:%d", newProvObj->connection->addr->addr, newProvObj->connection->addr->port);
     obj->data = (void *) NSCopyProvider_internal(newProvObj);
@@ -248,7 +247,7 @@ NSResult NSConsumerCacheWriteProvider(NSCacheList * list, NSCacheElement * newOb
     if (!obj->data)
     {
         NS_LOG (ERROR, "Failed to CopyProvider");
-        pthread_mutex_unlock(mutex);
+        oc_mutex_unlock(*mutex);
 
         return NS_ERROR;
     }
@@ -259,7 +258,7 @@ NSResult NSConsumerCacheWriteProvider(NSCacheList * list, NSCacheElement * newOb
         list->head = obj;
         list->tail = obj;
 
-        pthread_mutex_unlock(mutex);
+        oc_mutex_unlock(*mutex);
 
         return NS_OK;
     }
@@ -267,7 +266,7 @@ NSResult NSConsumerCacheWriteProvider(NSCacheList * list, NSCacheElement * newOb
     (list->tail)->next = obj;
     list->tail = obj;
 
-    pthread_mutex_unlock(mutex);
+    oc_mutex_unlock(*mutex);
 
     return NS_OK;
 }
@@ -276,8 +275,8 @@ NSCacheElement * NSPopProviderCacheList(NSCacheList * list)
 {
     NS_VERIFY_NOT_NULL(list, NULL);
 
-    pthread_mutex_t * mutex = NSGetCacheMutex();
-    pthread_mutex_lock(mutex);
+    oc_mutex * mutex = NSGetCacheMutex();
+    oc_mutex_lock(*mutex);
 
     NSCacheElement * head = list->head;
     if (head)
@@ -291,7 +290,7 @@ NSCacheElement * NSPopProviderCacheList(NSCacheList * list)
         head->next = NULL;
     }
 
-    pthread_mutex_unlock(mutex);
+    oc_mutex_unlock(*mutex);
     return head;
 }
 
@@ -300,8 +299,8 @@ NSResult NSConsumerStorageDestroy(NSCacheList * list)
 {
     NS_VERIFY_NOT_NULL(list, NS_ERROR);
 
-    pthread_mutex_t * mutex = NSGetCacheMutex();
-    pthread_mutex_lock(mutex);
+    oc_mutex * mutex = NSGetCacheMutex();
+    oc_mutex_lock(*mutex);
 
     NSCacheElement * iter = list->head;
     NSCacheElement * next = NULL;
@@ -323,7 +322,7 @@ NSResult NSConsumerStorageDestroy(NSCacheList * list)
         NSOICFree(list);
     }
 
-    pthread_mutex_unlock(mutex);
+    oc_mutex_unlock(*mutex);
 
     return NS_OK;
 }
index fc7aaaf..c4312b2 100644 (file)
@@ -27,7 +27,7 @@ extern "C" {
 
 #include <stdlib.h>
 #include <stdbool.h>
-#include <pthread.h>
+#include "octhread.h"
 #include "NSConsumerCommon.h"
 
 NSCacheList * NSConsumerStorageCreate();
@@ -36,7 +36,7 @@ NSResult NSConsumerStorageWrite(NSCacheList * list, NSCacheElement * newObj);
 NSResult NSConsumerStorageDelete(NSCacheList * list, const char * delId);
 NSResult NSConsumerStorageDestroy(NSCacheList * list);
 
-pthread_mutex_t * NSGetCacheMutex();
+oc_mutex * NSGetCacheMutex();
 
 bool NSConsumerCompareIdCacheData(NSCacheType type, void * data, const char * id);
 
index 200661f..049a8d4 100644 (file)
 
 #include <stdlib.h>
 #include <stdbool.h>
+
+#ifdef SYSTEM_WINDOWS
+#include "win_sleep.h"
+#else
 #include <unistd.h>
+#endif
 
 #include "oic_malloc.h"
 #include "oic_string.h"
@@ -147,7 +152,6 @@ void * NSConsumerMsgHandleThreadFunc(void * threadHandle)
         if (!queue)
         {
             queue = *(NSGetMsgHandleQueue());
-            usleep(2000);
             continue;
         }
 
index 36655d8..1f8127c 100644 (file)
 #include <memory.h>
 #include "oic_malloc.h"
 
-static pthread_mutex_t g_create_mutex;
+#ifndef SYSTEM_WINDOWS
+#include <pthread.h>
+
+typedef struct
+{
+    pthread_mutex_t mutex;
+} oc_mutex_ns_internal;
+#endif
 
 NSConsumerThread * NSThreadInit(NSThreadFunc func, void * data)
 {
     NS_VERIFY_NOT_NULL(func, NULL);
 
-    pthread_mutex_init(&g_create_mutex, NULL);
-
     NSConsumerThread * handle = (NSConsumerThread *)OICMalloc(sizeof(NSConsumerThread));
     NS_VERIFY_NOT_NULL(handle, NULL);
 
     memset(handle, 0, sizeof(NSConsumerThread));
 
-    pthread_mutexattr_init(&(handle->mutex_attr));
-
-    int pthreadResult = pthread_mutexattr_settype(&(handle->mutex_attr), PTHREAD_MUTEX_RECURSIVE);
-    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(pthreadResult == 0 ? (void *)1 : NULL,
-            NULL, NSDestroyThreadHandle(handle));
-
-    pthreadResult = pthread_mutex_init(&(handle->mutex), &(handle->mutex_attr));
-    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(pthreadResult == 0 ? (void *)1 : NULL,
-            NULL, NSDestroyThreadHandle(handle));
-
-    pthread_mutex_lock(&g_create_mutex);
+#ifndef SYSTEM_WINDOWS
+    oc_mutex_ns_internal * mutexInfo
+        = (oc_mutex_ns_internal *) OICMalloc(sizeof(oc_mutex_ns_internal));
+    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(mutexInfo, NULL, NSDestroyThreadHandle(handle));
+
+    pthread_mutexattr_t * mutex_attr
+        = (pthread_mutexattr_t *) OICMalloc(sizeof(pthread_mutexattr_t));
+    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(mutex_attr, NULL,
+              {
+                  NSOICFree(mutexInfo);
+                  NSDestroyThreadHandle(handle));
+              }
+    int pthreadResult = pthread_mutexattr_settype(mutex_attr, PTHREAD_MUTEX_RECURSIVE);
+    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(pthreadResult == 0 ? (void *)1 : NULL, NULL,
+            {
+                    NSOICFree(mutexInfo);
+                    NSOICFree(mutex_attr);
+                    NSDestroyThreadHandle(handle);
+            });
+
+    pthreadResult = pthread_mutex_init(& (mutexInfo->mutex), mutex_attr);
+    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(pthreadResult == 0 ? (void *)1 : NULL, NULL,
+            {
+                    NSOICFree(mutexInfo);
+                    pthread_mutexattr_destroy(mutex_attr);
+                    NSOICFree(mutex_attr);
+                    NSDestroyThreadHandle(handle);
+            });
+    pthread_mutexattr_destroy(mutex_attr);
+    NSOICFree(mutex_attr);
+    handle->mutex = (oc_mutex) mutexInfo;
+#else
+    handle->mutex = oc_mutex_new();
+    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(handle->mutex, NULL, NSDestroyThreadHandle(handle));
+#endif
 
     handle->isStarted = true;
 
-    pthread_attr_t attrDetached = {};
-    pthread_attr_init(& attrDetached);
-    pthread_attr_setdetachstate(& attrDetached, PTHREAD_CREATE_DETACHED);
-
-    pthreadResult = pthread_create(&(handle->thread_id), & attrDetached, func,
-                           (data == NULL) ? (void *) handle : (void *)data);
-    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(pthreadResult == 0 ? (void *)1 : NULL,
-            NULL, NSDestroyThreadHandle(handle));
-
-    pthread_attr_destroy(& attrDetached);
-
-    pthread_mutex_unlock(&g_create_mutex);
+    OCThreadResult_t ret = oc_thread_new(& handle->thread_id, func,
+                             (data == NULL) ? (void *) handle : (void *)data);
+    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(ret == OC_THREAD_SUCCESS ? (void*)1 : NULL, NULL,
+            NSDestroyThreadHandle(handle));
 
     return handle;
 }
@@ -73,49 +94,39 @@ void NSThreadLock(NSConsumerThread * handle)
 {
     NS_VERIFY_NOT_NULL_V(handle);
 
-    pthread_mutex_lock(&(handle->mutex));
+    oc_mutex_lock(handle->mutex);
 }
 
 void NSThreadUnlock(NSConsumerThread * handle)
 {
     NS_VERIFY_NOT_NULL_V(handle);
 
-    pthread_mutex_unlock(&(handle->mutex));
-}
-
-void NSThreadStop(NSConsumerThread * handle)
-{
-    NS_VERIFY_NOT_NULL_V(handle);
-
-    handle->isStarted = false;
-    NSThreadJoin(handle);
-
-    NSDestroyThreadHandle(handle);
+    oc_mutex_unlock(handle->mutex);
 }
 
 void NSThreadJoin(NSConsumerThread * handle)
 {
     NS_VERIFY_NOT_NULL_V(handle);
 
-    if (handle->thread_id)
+    if (handle->isStarted)
     {
-        void * retData = NULL;
-        pthread_join(handle->thread_id, & retData);
-        NSOICFree(retData);
+        oc_thread_wait(handle->thread_id);
     }
 }
 
-void NSDestroyThreadHandle(NSConsumerThread * handle)
+void NSThreadStop(NSConsumerThread * handle)
 {
     NS_VERIFY_NOT_NULL_V(handle);
 
-    pthread_mutex_destroy(&(handle->mutex));
-    pthread_mutexattr_destroy(&(handle->mutex_attr));
+    handle->isStarted = false;
+    NSThreadJoin(handle);
 
-    pthread_mutex_unlock(&g_create_mutex);
+    NSDestroyThreadHandle(handle);
 }
 
-void NSThreadDetach()
+void NSDestroyThreadHandle(NSConsumerThread * handle)
 {
-    pthread_detach(pthread_self());
+    NS_VERIFY_NOT_NULL_V(handle);
+
+    oc_mutex_free(handle->mutex);
 }
index a78fb5a..88e9b39 100644 (file)
@@ -26,27 +26,15 @@ extern "C" {
 #endif // __cplusplus
 
 #include <stdbool.h>
-
-typedef enum
-{
-    pthread
-} NS_THREAD_MODEL;
-
-#define NS_THREAD pthread
-
-#if (NS_THREAD == pthread)
-#include <pthread.h>
+#include "octhread.h"
 
 typedef struct
 {
     bool isStarted;
-    pthread_t thread_id;
-    pthread_mutex_t mutex;
-    pthread_mutexattr_t mutex_attr;
+    oc_thread thread_id;
+    oc_mutex mutex;
 } NSConsumerThread;
 
-#endif
-
 typedef void *(*NSThreadFunc)(void *);
 
 NSConsumerThread * NSThreadInit(NSThreadFunc, void *);
@@ -61,8 +49,6 @@ void NSThreadStop(NSConsumerThread *);
 
 void NSDestroyThreadHandle(NSConsumerThread *);
 
-void NSThreadDetach();
-
 #ifdef __cplusplus
 }
 #endif // __cplusplus