Skip to content

Commit 6c93eb5

Browse files
committed
refactor: move new fields behind preprocessing macro
Move cancellation mutex lock and await stop flag behind SYNC or RUNTIME selection pre-processing macro checks.
1 parent d8421a1 commit 6c93eb5

File tree

5 files changed

+102
-5
lines changed

5 files changed

+102
-5
lines changed

core/pubnub_alloc_static.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ void pballoc_free_at_last(pubnub_t *pb)
5858
pbpal_free(pb);
5959
pubnub_mutex_unlock(pb->monitor);
6060
pubnub_mutex_destroy(pb->monitor);
61+
#if !defined(PUBNUB_CALLBACK_API) || defined(PUBNUB_NTF_RUNTIME_SELECTION)
6162
pubnub_mutex_destroy(pb->cancel_monitor);
63+
#endif
6264
#if PUBNUB_USE_AUTO_HEARTBEAT
6365
pubnub_mutex_destroy(pb->thumper_monitor);
6466
#endif

core/pubnub_alloc_std.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ void pballoc_free_at_last(pubnub_t* pb)
130130
remove_allocated(pb);
131131
pubnub_mutex_unlock(pb->monitor);
132132
pubnub_mutex_destroy(pb->monitor);
133+
#if !defined(PUBNUB_CALLBACK_API) || defined(PUBNUB_NTF_RUNTIME_SELECTION)
133134
pubnub_mutex_destroy(pb->cancel_monitor);
135+
#endif
134136
#if PUBNUB_USE_AUTO_HEARTBEAT
135137
pubnub_mutex_destroy(pb->thumper_monitor);
136138
#endif

core/pubnub_internal_common.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,14 @@ struct pubnub_ {
414414

415415
#if PUBNUB_THREADSAFE
416416
pubnub_mutex_t monitor;
417-
pubnub_mutex_t cancel_monitor;
417+
#endif
418+
#if !defined(PUBNUB_CALLBACK_API) || defined(PUBNUB_NTF_RUNTIME_SELECTION)
419+
#if PUBNUB_THREADSAFE
420+
pubnub_mutex_t cancel_monitor;
418421
#endif
419422
/** Whether sync `await` should stop (cancel) or not. */
420423
bool should_stop_await;
424+
#endif
421425

422426
#if PUBNUB_TIMERS_API
423427
/** Duration of the transaction timeout, in milliseconds */

core/pubnub_pubsubapi.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ pubnub_t* pubnub_init(pubnub_t* p, const char* publish_key, const char* subscrib
1919
PUBNUB_ASSERT(pb_valid_ctx_ptr(p));
2020

2121
pubnub_mutex_init(p->monitor);
22+
#if !defined(PUBNUB_CALLBACK_API) || defined(PUBNUB_NTF_RUNTIME_SELECTION)
2223
pubnub_mutex_init(p->cancel_monitor);
24+
#endif
2325
pubnub_mutex_lock(p->monitor);
2426
pbcc_init(&p->core, publish_key, subscribe_key);
2527
if (PUBNUB_TIMERS_API) {
@@ -82,7 +84,9 @@ pubnub_t* pubnub_init(pubnub_t* p, const char* publish_key, const char* subscrib
8284
p->state = PBS_IDLE;
8385
p->trans = PBTT_NONE;
8486
p->options.use_http_keep_alive = true;
87+
#if !defined(PUBNUB_CALLBACK_API) || defined(PUBNUB_NTF_RUNTIME_SELECTION)
8588
p->should_stop_await = false;
89+
#endif
8690
#if PUBNUB_USE_IPV6
8791
/* IPv4 connectivity type by default. */
8892
p->options.ipv6_connectivity = false;
@@ -250,9 +254,11 @@ enum pubnub_cancel_res pubnub_cancel(pubnub_t* pb)
250254
enum pubnub_cancel_res res = PN_CANCEL_STARTED;
251255
PUBNUB_ASSERT(pb_valid_ctx_ptr(pb));
252256

257+
#if !defined(PUBNUB_CALLBACK_API) || defined(PUBNUB_NTF_RUNTIME_SELECTION)
253258
pubnub_mutex_lock(pb->cancel_monitor);
254259
pb->should_stop_await = true;
255260
pubnub_mutex_unlock(pb->cancel_monitor);
261+
#endif
256262

257263
pubnub_mutex_lock(pb->monitor);
258264
pbnc_stop(pb, PNR_CANCELLED);

core/samples/pubnub_sync_sample.c

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,24 @@
55
#include "core/pubnub_helper.h"
66
#include "core/pubnub_timers.h"
77

8+
#if defined _WIN32
9+
#include <windows.h>
10+
#include <process.h>
11+
#else
12+
#include <pthread.h>
13+
#endif
14+
815
#include <stdio.h>
916
#include <time.h>
17+
#include <unistd.h>
18+
1019

11-
12-
static void generate_user_id(pubnub_t* pbp)
20+
static void generate_user_id(pubnub_t* pbp)
1321
{
1422
char const* user_id_default = "zeka-peka-iz-jendeka";
1523
struct Pubnub_UUID uuid;
16-
static struct Pubnub_UUID_String str_uuid;
17-
24+
static struct Pubnub_UUID_String str_uuid;
25+
1826
if (0 != pubnub_generate_uuid_v4_random(&uuid)) {
1927
pubnub_set_user_id(pbp, user_id_default);
2028
}
@@ -67,9 +75,71 @@ static int do_time(pubnub_t* pbp)
6775
return 0;
6876
}
6977

78+
static
79+
#if defined _WIN32
80+
void
81+
#else
82+
void*
83+
#endif
84+
subscribe_thr(void *pn_sub_addr)
85+
{
86+
pubnub_t *pbp = (pubnub_t *)pn_sub_addr;
87+
char const* chan = "hello_world";
88+
char const* msg;
89+
time_t t0;
90+
enum pubnub_res res;
91+
92+
puts("Subscribing...");
93+
time(&t0);
94+
res = pubnub_subscribe(pbp, chan, NULL);
95+
if (PNR_STARTED == res) {
96+
res = pubnub_await(pbp);
97+
}
98+
printf("Subscribe/connect lasted %lf seconds.\n", difftime(time(NULL), t0));
99+
if (PNR_OK == res) {
100+
puts("Subscribed!");
101+
}
102+
else {
103+
printf("Subscribing failed with code: %d('%s')\n",
104+
res,
105+
pubnub_res_2_string(res));
106+
}
107+
108+
time(&t0);
109+
res = pubnub_subscribe(pbp, chan, NULL);
110+
if (PNR_STARTED == res) {
111+
res = pubnub_await(pbp);
112+
}
113+
printf("Subscribe lasted %lf seconds.\n", difftime(time(NULL), t0));
114+
if (PNR_OK == res) {
115+
puts("Subscribed! Got messages:");
116+
for (;;) {
117+
msg = pubnub_get(pbp);
118+
if (NULL == msg) {
119+
break;
120+
}
121+
puts(msg);
122+
}
123+
}
124+
else {
125+
printf("Subscribing failed with code: %d('%s')\n",
126+
res,
127+
pubnub_res_2_string(res));
128+
}
129+
130+
#if !defined _WIN32
131+
return NULL;
132+
#endif
133+
}
134+
70135

71136
int main()
72137
{
138+
#if defined _WIN32
139+
uintptr_t sub_thread;
140+
#else
141+
pthread_t sub_thread;
142+
#endif
73143
time_t t0;
74144
char const* msg;
75145
enum pubnub_res res;
@@ -94,6 +164,19 @@ int main()
94164

95165
pubnub_set_auth(pbp, "danaske");
96166

167+
#if defined _WIN32
168+
sub_thread = _beginthread(subscribe_thr, 0, pbp);
169+
#else
170+
pthread_create(&sub_thread, NULL, subscribe_thr, pbp);
171+
#endif
172+
puts("Lets go sleeping...");
173+
sleep(4);
174+
puts("Done sleeping. Cancelling subscribe long-poll...");
175+
pubnub_cancel(pbp);
176+
sleep(2);
177+
printf("What we have: %s\n", pubnub_res_2_string(pubnub_last_result(pbp)));
178+
return 0;
179+
97180
puts("Publishing...");
98181
time(&t0);
99182
res = pubnub_publish(pbp, chan, "\"Hello world from sync!\"");

0 commit comments

Comments
 (0)