Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

core: remove lock from c api #177

Merged
merged 1 commit into from
Oct 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions doc/examples.layer1.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ So far, we are using only 1 thread for serving the client requests because
thread pool, and in the configuration file, we have **[threadpool.THREAD_POOL_DEFAULT] worker_count
= 1**. To scale up the service, we increase the number of threads to 4. However, the service
handlers in **counter.service.impl.h** must also be made thread safe, by using a lock with type
**::dsn::service::zrwlock_nr**. In rNET_HDR_DSN, all non-deterministic behaviors must be implemented
**::dsn::zrwlock_nr**. In rNET_HDR_DSN, all non-deterministic behaviors must be implemented
using rDSN's service API (see
[here](https://github.com/Microsoft/rDSN/wiki/Programming-Rules-and-FAQ)). However, if you are in
favor of some other reader-writer locks, you can still use them by integrating them as rwlock
Expand All @@ -378,21 +378,21 @@ providers as we do with logging providers above.
10 virtual void on_add(const ::dsn::example::count_op& op, ::dsn::service::rpc_replier<int32_t>&
reply)
11 {
12 ::dsn::service::zauto_write_lock l(_lock);
12 ::dsn::zauto_write_lock l(_lock);
13 auto result = (_counters[op.name] += op.operand);
14 reply(result);
15 }
16
17 virtual void on_read(const std::string& name, ::dsn::service::rpc_replier<int32_t>& reply)
18 {
19 ::dsn::service::zauto_read_lock l(_lock);
19 ::dsn::zauto_read_lock l(_lock);
20 auto it = _counters.find(name);
21 auto result = it != _counters.end() ? it->second : 0;
22 reply(result);
23 }
24
25 private:
26 ::dsn::service::zrwlock_nr _lock;
26 ::dsn::zrwlock_nr _lock;
```

Another approach without using locks is to partition the counter name space so that the **ADD** and
Expand Down
52 changes: 0 additions & 52 deletions include/dsn/c/api_layer1.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,58 +293,6 @@ __inline uint64_t dsn_now_ms() { return dsn_now_ns() / 1000000; }

/*@}*/

/*!
@defgroup sync Thread Synchornization

Thread Synchornization Primitives

Note developers can easily plugin their own implementation to
replace the underneath implementation of these primitives.
@{
*/

/*!
@defgroup sync-exlock Exlusive Locks
Exlusive Locks
@{
*/

/*! create a recursive? or not exlusive lock*/
extern DSN_API dsn_handle_t dsn_exlock_create(bool recursive);
extern DSN_API void dsn_exlock_destroy(dsn_handle_t l);
extern DSN_API void dsn_exlock_lock(dsn_handle_t l);
extern DSN_API bool dsn_exlock_try_lock(dsn_handle_t l);
extern DSN_API void dsn_exlock_unlock(dsn_handle_t l);
/*@}*/

/*!
@defgroup sync-rwlock Non-recursive Read-Write Locks
Non-recursive Read-Write Locks
@{
*/
extern DSN_API dsn_handle_t dsn_rwlock_nr_create();
extern DSN_API void dsn_rwlock_nr_destroy(dsn_handle_t l);
extern DSN_API void dsn_rwlock_nr_lock_read(dsn_handle_t l);
extern DSN_API void dsn_rwlock_nr_unlock_read(dsn_handle_t l);
extern DSN_API bool dsn_rwlock_nr_try_lock_read(dsn_handle_t l);
extern DSN_API void dsn_rwlock_nr_lock_write(dsn_handle_t l);
extern DSN_API void dsn_rwlock_nr_unlock_write(dsn_handle_t l);
extern DSN_API bool dsn_rwlock_nr_try_lock_write(dsn_handle_t l);
/*@}*/

/*!
@defgroup sync-sema Semaphore
Semaphore
@{
*/
/*! create a semaphore with initial count equals to inital_count */
extern DSN_API dsn_handle_t dsn_semaphore_create(int initial_count);
extern DSN_API void dsn_semaphore_destroy(dsn_handle_t s);
extern DSN_API void dsn_semaphore_signal(dsn_handle_t s, int count);
extern DSN_API void dsn_semaphore_wait(dsn_handle_t s);
extern DSN_API bool dsn_semaphore_wait_timeout(dsn_handle_t s, int timeout_milliseconds);
/*@}*/

/*@}*/

/*@}*/
5 changes: 3 additions & 2 deletions include/dsn/dist/failure_detector/failure_detector.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*
* Revision history:
* Mar., 2015, @imzhenyu (Zhenyu Guo), first version
* Dec., 2015, @shengofsun (Weijie Sun), make service::zlock preoteced,
* Dec., 2015, @shengofsun (Weijie Sun), make zlock preoteced,
* give the subClasses flexibility
* xxxx-xx-xx, author, fix bug about xxx
*/
Expand Down Expand Up @@ -61,6 +61,7 @@
*/
#pragma once

#include <dsn/tool-api/zlocks.h>
#include <dsn/dist/failure_detector/fd.client.h>
#include <dsn/dist/failure_detector/fd.server.h>
#include <dsn/perf_counter/perf_counter_wrapper.h>
Expand Down Expand Up @@ -214,7 +215,7 @@ class failure_detector : public failure_detector_service,
perf_counter_wrapper _recent_beacon_fail_count;

protected:
mutable service::zlock _lock;
mutable zlock _lock;
dsn::task_tracker _tracker;

// subClass can rewrite these method.
Expand Down
4 changes: 2 additions & 2 deletions include/dsn/dist/failure_detector_multimaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
#pragma once

#include <dsn/tool-api/group_address.h>
#include <dsn/tool-api/zlocks.h>
#include <dsn/dist/failure_detector.h>
#include <dsn/cpp/zlocks.h>
#include <functional>

namespace dsn {
Expand Down Expand Up @@ -81,7 +81,7 @@ class slave_failure_detector_with_multimaster : public dsn::fd::failure_detector
//------------------ inline implementation --------------------------------
inline ::dsn::rpc_address slave_failure_detector_with_multimaster::current_server_contact() const
{
service::zauto_lock l(failure_detector::_lock);
zauto_lock l(failure_detector::_lock);
return _meta_servers.group_address()->leader();
}
}
Expand Down
1 change: 0 additions & 1 deletion include/dsn/service_api_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#include <dsn/tool-api/auto_codes.h>
#include <dsn/cpp/serialization.h>
#include <dsn/cpp/rpc_stream.h>
#include <dsn/cpp/zlocks.h>
#include <dsn/cpp/serverlet.h>
#include <dsn/cpp/service_app.h>
#include <dsn/tool-api/rpc_address.h>
8 changes: 0 additions & 8 deletions include/dsn/tool-api/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,6 @@

namespace dsn {

namespace lock_checker {
extern __thread int zlock_exclusive_count;
extern __thread int zlock_shared_count;
extern void check_wait_safety();
extern void check_dangling_lock();
extern void check_wait_task(task *waitee);
}

class task_worker;
class task_worker_pool;
class service_node;
Expand Down
4 changes: 2 additions & 2 deletions include/dsn/tool-api/uri_address.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include <dsn/utility/synchronize.h>
#include <dsn/tool-api/rpc_address.h>
#include <dsn/tool-api/partition_resolver.h>
#include <dsn/cpp/zlocks.h>
#include <dsn/tool-api/zlocks.h>

namespace dsn {
/** A RPC URI address. */
Expand Down Expand Up @@ -109,7 +109,7 @@ class uri_resolver
private:
std::unordered_map<std::string, dist::partition_resolver_ptr>
_apps; ///< app-path to app-resolver map
service::zrwlock_nr _apps_lock;
zrwlock_nr _apps_lock;

rpc_address _meta_server;
std::string _name;
Expand Down
135 changes: 54 additions & 81 deletions include/dsn/cpp/zlocks.h → include/dsn/tool-api/zlocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,107 +24,72 @@
* THE SOFTWARE.
*/

/*
* Description:
* lock implementation atop c service api
*
* Revision history:
* Mar., 2015, @imzhenyu (Zhenyu Guo), first version
* xxxx-xx-xx, author, fix bug about xxx
*/

#pragma once

#include <dsn/service_api_c.h>
#include <dsn/utility/utils.h>
#include <atomic>
#include <algorithm>
#include <dsn/utility/utils.h>

namespace dsn {
namespace service {
///
/// synchronization objects of rDSN.
///
/// you MUST always use these objects to do synchronization when you write code
/// in rdsn's "service_app", because different implementations may be provided
/// when then program is running in different mode(nativerun/simulator).
///
/// As for the synchronize objects in "utility/synchronize.h", they are
/// used for synchronization inner the rdsn core runtime.
///

/*!
@addtogroup sync-exlock
@{
*/
namespace dsn {
class ilock;
class zlock
{
public:
zlock(bool recursive = false) { _h = dsn_exlock_create(recursive); }
~zlock() { dsn_exlock_destroy(_h); }
zlock(bool recursive = false);
~zlock();

void lock() { dsn_exlock_lock(_h); }
bool try_lock() { return dsn_exlock_try_lock(_h); }
void unlock() { dsn_exlock_unlock(_h); }
void lock();
bool try_lock();
void unlock();

private:
dsn_handle_t _h;

private:
// no assignment operator
zlock &operator=(const zlock &source);
zlock(const zlock &source);
DISALLOW_COPY_AND_ASSIGN(zlock);
ilock *_h;
};
/*@}*/

/*!
@addtogroup sync-rwlock
@{
*/
class rwlock_nr_provider;
class zrwlock_nr
{
public:
zrwlock_nr() { _h = dsn_rwlock_nr_create(); }
~zrwlock_nr() { dsn_rwlock_nr_destroy(_h); }
zrwlock_nr();
~zrwlock_nr();

void lock_read() { dsn_rwlock_nr_lock_read(_h); }
void unlock_read() { dsn_rwlock_nr_unlock_read(_h); }
bool try_lock_read() { return dsn_rwlock_nr_try_lock_read(_h); }
void lock_read();
void unlock_read();
bool try_lock_read();

void lock_write() { dsn_rwlock_nr_lock_write(_h); }
void unlock_write() { dsn_rwlock_nr_unlock_write(_h); }
bool try_lock_write() { return dsn_rwlock_nr_try_lock_write(_h); }

private:
dsn_handle_t _h;
void lock_write();
void unlock_write();
bool try_lock_write();

private:
// no assignment operator
zrwlock_nr &operator=(const zrwlock_nr &source);
zrwlock_nr(const zrwlock_nr &source);
DISALLOW_COPY_AND_ASSIGN(zrwlock_nr);
rwlock_nr_provider *_h;
};
/*@}*/

/*!
@addtogroup sync-sema
@{
*/
class semaphore_provider;
class zsemaphore
{
public:
zsemaphore(int initial_count = 0) { _h = dsn_semaphore_create(initial_count); }
~zsemaphore() { dsn_semaphore_destroy(_h); }
zsemaphore(int initial_count = 0);
~zsemaphore();

public:
virtual void signal(int count = 1) { dsn_semaphore_signal(_h, count); }

virtual bool wait(int timeout_milliseconds = TIME_MS_MAX)
{
if (static_cast<unsigned int>(timeout_milliseconds) == TIME_MS_MAX) {
dsn_semaphore_wait(_h);
return true;
} else {
return dsn_semaphore_wait_timeout(_h, timeout_milliseconds);
}
}

private:
dsn_handle_t _h;
void signal(int count = 1);
bool wait(int timeout_milliseconds = TIME_MS_MAX);

private:
// no assignment operator
zsemaphore &operator=(const zsemaphore &source);
zsemaphore(const zsemaphore &source);
DISALLOW_COPY_AND_ASSIGN(zsemaphore);
semaphore_provider *_h;
};

class zevent
Expand All @@ -133,23 +98,22 @@ class zevent
zevent(bool manualReset, bool initState = false);
~zevent();

public:
void set();
void reset();
bool wait(int timeout_milliseconds = TIME_MS_MAX);

private:
DISALLOW_COPY_AND_ASSIGN(zevent);
zsemaphore _sema;
std::atomic<bool> _signaled;
bool _manualReset;

private:
// no assignment operator
zevent &operator=(const zevent &source);
zevent(const zevent &source);
};
/*@}*/
}

///
/// RAII wrapper of rdsn's synchronization objects
///
namespace dsn {
class zauto_lock
{
public:
Expand Down Expand Up @@ -222,4 +186,13 @@ class zauto_write_lock
zrwlock_nr *_lock;
};
}
} // end namespace dsn::service

///
/// utils function used to check the lock safety
///
namespace dsn {
namespace lock_checker {
void check_wait_safety();
void check_dangling_lock();
}
}
12 changes: 0 additions & 12 deletions include/dsn/tool_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ Component providers define the interface for the local components (e.g., network
#include <dsn/tool-api/network.h>
#include <dsn/tool-api/aio_provider.h>
#include <dsn/tool-api/env_provider.h>
#include <dsn/tool-api/zlock_provider.h>
#include <dsn/tool-api/message_parser.h>
#include <dsn/tool-api/logging_provider.h>
#include <dsn/tool-api/timer_service.h>
Expand Down Expand Up @@ -139,17 +138,6 @@ DSN_API bool register_component_provider(const char *name,
admission_controller::factory f,
::dsn::provider_type type);
DSN_API bool
register_component_provider(const char *name, lock_provider::factory f, ::dsn::provider_type type);
DSN_API bool register_component_provider(const char *name,
lock_nr_provider::factory f,
::dsn::provider_type type);
DSN_API bool register_component_provider(const char *name,
rwlock_nr_provider::factory f,
::dsn::provider_type type);
DSN_API bool register_component_provider(const char *name,
semaphore_provider::factory f,
::dsn::provider_type type);
DSN_API bool
register_component_provider(const char *name, network::factory f, ::dsn::provider_type type);
DSN_API bool
register_component_provider(const char *name, aio_provider::factory f, ::dsn::provider_type type);
Expand Down
Loading