mk_thread.cpp 3.05 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
3 4 5
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
xiongziliang committed
6 7 8
 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
xiongziliang committed
9 10 11 12 13 14
 */

#include "mk_thread.h"
#include "mk_tcp_private.h"
#include "Util/logger.h"
#include "Poller/EventPoller.h"
15
#include "Thread/WorkThreadPool.h"
xiongziliang committed
16 17 18 19 20
using namespace std;
using namespace toolkit;

API_EXPORT mk_thread API_CALL mk_thread_from_tcp_session(mk_tcp_session ctx){
    assert(ctx);
21
    TcpSessionForC *obj = (TcpSessionForC *)ctx;
xiongziliang committed
22 23 24 25 26
    return obj->getPoller().get();
}

API_EXPORT mk_thread API_CALL mk_thread_from_tcp_client(mk_tcp_client ctx){
    assert(ctx);
27
    TcpClientForC::Ptr *client = (TcpClientForC::Ptr *)ctx;
xiongziliang committed
28 29 30
    return (*client)->getPoller().get();
}

31 32 33 34 35 36 37 38
API_EXPORT mk_thread API_CALL mk_thread_from_pool(){
    return EventPollerPool::Instance().getPoller().get();
}

API_EXPORT mk_thread API_CALL mk_thread_from_pool_work(){
    return WorkThreadPool::Instance().getPoller().get();
}

xiongziliang committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
API_EXPORT void API_CALL mk_async_do(mk_thread ctx,on_mk_async cb, void *user_data){
    assert(ctx && cb);
    EventPoller *poller = (EventPoller *)ctx;
    poller->async([cb,user_data](){
        cb(user_data);
    });
}

API_EXPORT void API_CALL mk_sync_do(mk_thread ctx,on_mk_async cb, void *user_data){
    assert(ctx && cb);
    EventPoller *poller = (EventPoller *)ctx;
    poller->sync([cb,user_data](){
        cb(user_data);
    });
}

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
class TimerForC : public std::enable_shared_from_this<TimerForC>{
public:
    typedef std::shared_ptr<TimerForC> Ptr;

    TimerForC(on_mk_timer cb, void *user_data){
        _cb = cb;
        _user_data = user_data;
    }

    ~TimerForC(){}

    uint64_t operator()(){
        lock_guard<recursive_mutex> lck(_mxt);
        if(!_cb){
            return 0;
        }
        return _cb(_user_data);
    }

    void cancel(){
        lock_guard<recursive_mutex> lck(_mxt);
        _cb = nullptr;
        _task->cancel();
    }

    void start(int ms ,EventPoller &poller){
        weak_ptr<TimerForC> weak_self = shared_from_this();
82
        _task = poller.doDelayTask(ms, [weak_self]() {
83
            auto strong_self = weak_self.lock();
84 85
            if (!strong_self) {
                return (uint64_t) 0;
86 87 88 89 90 91 92 93 94 95 96
            }
            return (*strong_self)();
        });
    }
private:
    on_mk_timer _cb = nullptr;
    void *_user_data = nullptr;
    recursive_mutex _mxt;
    DelayTask::Ptr _task;
};

xiongziliang committed
97 98 99
API_EXPORT mk_timer API_CALL mk_timer_create(mk_thread ctx,uint64_t delay_ms,on_mk_timer cb, void *user_data){
    assert(ctx && cb);
    EventPoller *poller = (EventPoller *)ctx;
100 101 102
    TimerForC::Ptr *ret = new TimerForC::Ptr(new TimerForC(cb, user_data));
    (*ret)->start(delay_ms,*poller);
    return ret;
xiongziliang committed
103 104 105 106
}

API_EXPORT void API_CALL mk_timer_release(mk_timer ctx){
    assert(ctx);
107
    TimerForC::Ptr *obj = (TimerForC::Ptr *)ctx;
xiongziliang committed
108 109 110
    (*obj)->cancel();
    delete obj;
}