Skip to content

Commit 0658544

Browse files
authored
Merge pull request #7207 from mikaleppanen/dns_singletons
Change DNS global class definitions to singletons
2 parents c964f2e + 9457bd1 commit 0658544

File tree

1 file changed

+37
-36
lines changed

1 file changed

+37
-36
lines changed

features/netsocket/nsapi_dns.cpp

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "OnboardNetworkStack.h"
2929
#include "Kernel.h"
3030
#include "PlatformMutex.h"
31+
#include "SingletonPtr.h"
3132

3233
#define CLASS_IN 1
3334

@@ -115,10 +116,10 @@ static uint16_t dns_message_id = 1;
115116
static int dns_unique_id = 1;
116117
static DNS_QUERY *dns_query_queue[DNS_QUERY_QUEUE_SIZE];
117118
// Protects cache shared between blocking and asynchronous calls
118-
static PlatformMutex dns_cache_mutex;
119+
static SingletonPtr<PlatformMutex> dns_cache_mutex;
119120
// Protects from several threads running asynchronous DNS
120-
static PlatformMutex dns_mutex;
121-
static call_in_callback_cb_t dns_call_in = 0;
121+
static SingletonPtr<PlatformMutex> dns_mutex;
122+
static SingletonPtr<call_in_callback_cb_t> dns_call_in;
122123
static bool dns_timer_running = false;
123124

124125
// DNS server configuration
@@ -315,7 +316,7 @@ static void nsapi_dns_cache_add(const char *host, nsapi_addr_t *address, uint32_
315316
return;
316317
}
317318

318-
dns_cache_mutex.lock();
319+
dns_cache_mutex->lock();
319320

320321
int index = -1;
321322
uint64_t accessed = UINT64_MAX;
@@ -332,7 +333,7 @@ static void nsapi_dns_cache_add(const char *host, nsapi_addr_t *address, uint32_
332333
}
333334

334335
if (index < 0) {
335-
dns_cache_mutex.unlock();
336+
dns_cache_mutex->unlock();
336337
return;
337338
}
338339

@@ -352,14 +353,14 @@ static void nsapi_dns_cache_add(const char *host, nsapi_addr_t *address, uint32_
352353
dns_cache[index]->accessed = ms_count;
353354
}
354355

355-
dns_cache_mutex.unlock();
356+
dns_cache_mutex->unlock();
356357
}
357358

358359
static nsapi_error_t nsapi_dns_cache_find(const char *host, nsapi_version_t version, nsapi_addr_t *address)
359360
{
360361
nsapi_error_t ret_val = NSAPI_ERROR_NO_ADDRESS;
361362

362-
dns_cache_mutex.lock();
363+
dns_cache_mutex->lock();
363364

364365
for (int i = 0; i < MBED_CONF_NSAPI_DNS_CACHE_SIZE; i++) {
365366
if (dns_cache[i]) {
@@ -380,7 +381,7 @@ static nsapi_error_t nsapi_dns_cache_find(const char *host, nsapi_version_t vers
380381
}
381382
}
382383

383-
dns_cache_mutex.unlock();
384+
dns_cache_mutex->unlock();
384385

385386
return ret_val;
386387
}
@@ -578,13 +579,13 @@ nsapi_value_or_error_t nsapi_dns_query_async(NetworkStack *stack, const char *ho
578579

579580
void nsapi_dns_call_in_set(call_in_callback_cb_t callback)
580581
{
581-
dns_call_in = callback;
582+
*dns_call_in.get() = callback;
582583
}
583584

584585
nsapi_error_t nsapi_dns_call_in(call_in_callback_cb_t cb, int delay, mbed::Callback<void()> func)
585586
{
586-
if (dns_call_in) {
587-
dns_call_in(delay, func);
587+
if (*dns_call_in.get()) {
588+
dns_call_in->call(delay, func);
588589
} else {
589590
return cb(delay, func);
590591
}
@@ -595,7 +596,7 @@ nsapi_value_or_error_t nsapi_dns_query_multiple_async(NetworkStack *stack, const
595596
NetworkStack::hostbyname_cb_t callback, nsapi_size_t addr_count,
596597
call_in_callback_cb_t call_in_cb, nsapi_version_t version)
597598
{
598-
dns_mutex.lock();
599+
dns_mutex->lock();
599600

600601
if (!stack) {
601602
return NSAPI_ERROR_PARAMETER;
@@ -604,14 +605,14 @@ nsapi_value_or_error_t nsapi_dns_query_multiple_async(NetworkStack *stack, const
604605
// check for valid host name
605606
int host_len = host ? strlen(host) : 0;
606607
if (host_len > DNS_HOST_NAME_MAX_LEN || host_len == 0) {
607-
dns_mutex.unlock();
608+
dns_mutex->unlock();
608609
return NSAPI_ERROR_PARAMETER;
609610
}
610611

611612
nsapi_addr address;
612613
if (nsapi_dns_cache_find(host, version, &address) == NSAPI_ERROR_OK) {
613614
SocketAddress addr(address);
614-
dns_mutex.unlock();
615+
dns_mutex->unlock();
615616
callback(NSAPI_ERROR_OK, &addr);
616617
return NSAPI_ERROR_OK;
617618
}
@@ -626,21 +627,21 @@ nsapi_value_or_error_t nsapi_dns_query_multiple_async(NetworkStack *stack, const
626627
}
627628

628629
if (index < 0) {
629-
dns_mutex.unlock();
630+
dns_mutex->unlock();
630631
return NSAPI_ERROR_NO_MEMORY;
631632
}
632633

633634
DNS_QUERY *query = new (std::nothrow) DNS_QUERY;
634635

635636
if (!query) {
636-
dns_mutex.unlock();
637+
dns_mutex->unlock();
637638
return NSAPI_ERROR_NO_MEMORY;
638639
}
639640

640641
query->host = new (std::nothrow) char[host_len + 1];
641642
if (!query->host) {
642643
delete query;
643-
dns_mutex.unlock();
644+
dns_mutex->unlock();
644645
return NSAPI_ERROR_NO_MEMORY;
645646
}
646647
strcpy(query->host, host);
@@ -689,7 +690,7 @@ nsapi_value_or_error_t nsapi_dns_query_multiple_async(NetworkStack *stack, const
689690
if (nsapi_dns_call_in(query->call_in_cb, DNS_TIMER_TIMEOUT, mbed::callback(nsapi_dns_query_async_timeout)) != NSAPI_ERROR_OK) {
690691
delete query->host;
691692
delete query;
692-
dns_mutex.unlock();
693+
dns_mutex->unlock();
693694
return NSAPI_ERROR_NO_MEMORY;
694695
}
695696
dns_timer_running = true;
@@ -698,7 +699,7 @@ nsapi_value_or_error_t nsapi_dns_query_multiple_async(NetworkStack *stack, const
698699
// Initiates query
699700
nsapi_dns_query_async_initiate_next();
700701

701-
dns_mutex.unlock();
702+
dns_mutex->unlock();
702703

703704
return query->unique_id;
704705
}
@@ -732,7 +733,7 @@ static void nsapi_dns_query_async_initiate_next(void)
732733

733734
static void nsapi_dns_query_async_timeout(void)
734735
{
735-
dns_mutex.lock();
736+
dns_mutex->lock();
736737

737738
DNS_QUERY *query = NULL;
738739

@@ -779,12 +780,12 @@ static void nsapi_dns_query_async_timeout(void)
779780
dns_timer_running = false;
780781
}
781782

782-
dns_mutex.unlock();
783+
dns_mutex->unlock();
783784
}
784785

785786
nsapi_error_t nsapi_dns_query_async_cancel(int id)
786787
{
787-
dns_mutex.lock();
788+
dns_mutex->lock();
788789

789790
DNS_QUERY *query = NULL;
790791

@@ -796,7 +797,7 @@ nsapi_error_t nsapi_dns_query_async_cancel(int id)
796797
}
797798

798799
if (!query || query->state == DNS_CANCELLED) {
799-
dns_mutex.unlock();
800+
dns_mutex->unlock();
800801
return NSAPI_ERROR_PARAMETER;
801802
}
802803

@@ -805,14 +806,14 @@ nsapi_error_t nsapi_dns_query_async_cancel(int id)
805806
// Do not call callback
806807
query->callback = 0;
807808

808-
dns_mutex.unlock();
809+
dns_mutex->unlock();
809810

810811
return NSAPI_ERROR_OK;
811812
}
812813

813814
static void nsapi_dns_query_async_create(void *ptr)
814815
{
815-
dns_mutex.lock();
816+
dns_mutex->lock();
816817

817818
int unique_id = reinterpret_cast<int>(ptr);
818819

@@ -827,7 +828,7 @@ static void nsapi_dns_query_async_create(void *ptr)
827828

828829
if (!query || query->state == DNS_CANCELLED) {
829830
// Cancel has been called
830-
dns_mutex.unlock();
831+
dns_mutex->unlock();
831832
return;
832833
}
833834

@@ -871,7 +872,7 @@ static void nsapi_dns_query_async_create(void *ptr)
871872
query->socket = socket;
872873
}
873874

874-
dns_mutex.unlock();
875+
dns_mutex->unlock();
875876

876877
nsapi_dns_query_async_send(reinterpret_cast<void *>(query->unique_id));
877878

@@ -926,7 +927,7 @@ static void nsapi_dns_query_async_resp(DNS_QUERY *query, nsapi_error_t status, S
926927
nsapi_dns_query_async_delete(query->unique_id);
927928
nsapi_dns_query_async_initiate_next();
928929

929-
dns_mutex.unlock();
930+
dns_mutex->unlock();
930931

931932
if (callback) {
932933
callback(status, address);
@@ -935,7 +936,7 @@ static void nsapi_dns_query_async_resp(DNS_QUERY *query, nsapi_error_t status, S
935936

936937
static void nsapi_dns_query_async_send(void *ptr)
937938
{
938-
dns_mutex.lock();
939+
dns_mutex->lock();
939940

940941
int unique_id = reinterpret_cast<int>(ptr);
941942

@@ -950,7 +951,7 @@ static void nsapi_dns_query_async_send(void *ptr)
950951

951952
if (!query || query->state != DNS_INITIATED) {
952953
// Cancel has been called
953-
dns_mutex.unlock();
954+
dns_mutex->unlock();
954955
return;
955956
}
956957

@@ -1004,7 +1005,7 @@ static void nsapi_dns_query_async_send(void *ptr)
10041005

10051006
query->socket_timeout = MBED_CONF_NSAPI_DNS_RESPONSE_WAIT_TIME;
10061007

1007-
dns_mutex.unlock();
1008+
dns_mutex->unlock();
10081009
}
10091010

10101011
static void nsapi_dns_query_async_socket_callback(void *ptr)
@@ -1020,7 +1021,7 @@ static void nsapi_dns_query_async_socket_callback_handle(NetworkStack *stack)
10201021
{
10211022
UDPSocket *socket = NULL;
10221023

1023-
dns_mutex.lock();
1024+
dns_mutex->lock();
10241025

10251026
for (int i = 0; i < DNS_QUERY_QUEUE_SIZE; i++) {
10261027
if (dns_query_queue[i] && dns_query_queue[i]->stack == stack) {
@@ -1033,7 +1034,7 @@ static void nsapi_dns_query_async_socket_callback_handle(NetworkStack *stack)
10331034
// create network packet
10341035
uint8_t *packet = (uint8_t *)malloc(DNS_BUFFER_SIZE);
10351036
if (!packet) {
1036-
dns_mutex.unlock();
1037+
dns_mutex->unlock();
10371038
return;
10381039
}
10391040

@@ -1085,12 +1086,12 @@ static void nsapi_dns_query_async_socket_callback_handle(NetworkStack *stack)
10851086
free(packet);
10861087
}
10871088

1088-
dns_mutex.unlock();
1089+
dns_mutex->unlock();
10891090
}
10901091

10911092
static void nsapi_dns_query_async_response(void *ptr)
10921093
{
1093-
dns_mutex.lock();
1094+
dns_mutex->lock();
10941095

10951096
int unique_id = reinterpret_cast<int>(ptr);
10961097

@@ -1126,6 +1127,6 @@ static void nsapi_dns_query_async_response(void *ptr)
11261127
nsapi_dns_query_async_resp(query, status, addresses);
11271128
delete[] addresses;
11281129
} else {
1129-
dns_mutex.unlock();
1130+
dns_mutex->unlock();
11301131
}
11311132
}

0 commit comments

Comments
 (0)