1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392 |
- #ifndef DASYNQ_H_
- #define DASYNQ_H_
- #include "dasynq/config.h"
- #include "dasynq/flags.h"
- #include "dasynq/stableheap.h"
- #include "dasynq/interrupt.h"
- #include "dasynq/util.h"
- #if DASYNQ_HAVE_EPOLL <= 0
- #if _POSIX_TIMERS > 0
- #include "dasynq/posixtimer.h"
- namespace dasynq {
- inline namespace v2 {
- template <typename T, bool provide_mono_timer = true> using timer_events = posix_timer_events<T, provide_mono_timer>;
- }
- }
- #else
- #include "dasynq/itimer.h"
- namespace dasynq {
- inline namespace v2 {
- template <typename T, bool provide_mono_timer = true> using timer_events = itimer_events<T, provide_mono_timer>;
- }
- }
- #endif
- #endif
- #if DASYNQ_HAVE_KQUEUE
- #if DASYNQ_KQUEUE_MACOS_WORKAROUND
- #include "dasynq/kqueue-macos.h"
- #include "dasynq/childproc.h"
- namespace dasynq {
- inline namespace v2 {
- template <typename T> using loop_t = macos_kqueue_loop<timer_events<child_proc_events<interrupt_channel<T>>, false>>;
- using loop_traits_t = macos_kqueue_traits;
- }
- }
- #else
- #include "dasynq/kqueue.h"
- #include "dasynq/childproc.h"
- namespace dasynq {
- inline namespace v2 {
- template <typename T> using loop_t = kqueue_loop<timer_events<child_proc_events<interrupt_channel<T>>, false>>;
- using loop_traits_t = kqueue_traits;
- }
- }
- #endif
- #elif DASYNQ_HAVE_EPOLL
- #include "dasynq/epoll.h"
- #include "dasynq/timerfd.h"
- #include "dasynq/childproc.h"
- namespace dasynq {
- inline namespace v2 {
- template <typename T> using loop_t = epoll_loop<interrupt_channel<timer_fd_events<child_proc_events<T>>>>;
- using loop_traits_t = epoll_traits;
- }
- }
- #else
- #include "dasynq/childproc.h"
- #if DASYNQ_HAVE_PSELECT
- #include "dasynq/pselect.h"
- namespace dasynq {
- inline namespace v2 {
- template <typename T> using loop_t = pselect_events<timer_events<interrupt_channel<child_proc_events<T>>, false>>;
- using loop_traits_t = select_traits;
- }
- }
- #else
- #include "dasynq/select.h"
- namespace dasynq {
- inline namespace v2 {
- template <typename T> using loop_t = select_events<timer_events<interrupt_channel<child_proc_events<T>>, false>>;
- using loop_traits_t = select_traits;
- }
- }
- #endif
- #endif
- #include <atomic>
- #include <condition_variable>
- #include <cstdint>
- #include <cstddef>
- #include <system_error>
- #include <unistd.h>
- #include <fcntl.h>
- #include "dasynq/mutex.h"
- #include "dasynq/basewatchers.h"
- namespace dasynq {
- enum class rearm
- {
-
- REARM,
-
- DISARM,
-
- NOOP,
-
- REMOVE,
-
- REMOVED,
-
- REQUEUE
- };
- class delayed_init {
- DASYNQ_EMPTY_BODY
- };
- inline namespace v2 {
- namespace dprivate {
-
-
-
- template <typename T_Mutex> class waitqueue;
- template <typename T_Mutex> class waitqueue_node;
-
-
-
- template <class T_Mutex> class condvar_selector;
- template <> class condvar_selector<std::mutex>
- {
- public:
- typedef std::condition_variable condvar;
- };
- template <class T_Mutex> class condvar_selector
- {
- public:
- typedef std::condition_variable_any condvar;
- };
-
- template <> class waitqueue_node<null_mutex>
- {
-
- friend class waitqueue<null_mutex>;
-
- public:
- void wait(std::unique_lock<null_mutex> &ul) { }
- void signal() { }
-
- DASYNQ_EMPTY_BODY
- };
- template <typename T_Mutex> class waitqueue_node
- {
- typename condvar_selector<T_Mutex>::condvar condvar;
- friend class waitqueue<T_Mutex>;
-
- waitqueue_node * next;
-
- public:
- void signal()
- {
- condvar.notify_one();
- }
-
- void wait(std::unique_lock<T_Mutex> &mutex_lock)
- {
- condvar.wait(mutex_lock);
- }
- };
- template <> class waitqueue<null_mutex>
- {
- public:
-
- waitqueue_node<null_mutex> * unqueue()
- {
- return nullptr;
- }
-
- waitqueue_node<null_mutex> * get_head()
- {
- return nullptr;
- }
-
- waitqueue_node<null_mutex> * get_second()
- {
- return nullptr;
- }
- bool check_head(waitqueue_node<null_mutex> &node)
- {
- return true;
- }
-
- bool is_empty()
- {
- return true;
- }
-
- void queue(waitqueue_node<null_mutex> *node)
- {
- }
- };
- template <typename T_Mutex> class waitqueue
- {
- waitqueue_node<T_Mutex> * tail = nullptr;
- waitqueue_node<T_Mutex> * head = nullptr;
- public:
-
- waitqueue_node<T_Mutex> * unqueue()
- {
- head = head->next;
- if (head == nullptr) {
- tail = nullptr;
- }
- return head;
- }
-
- waitqueue_node<T_Mutex> * get_head()
- {
- return head;
- }
-
- waitqueue_node<T_Mutex> * get_second()
- {
- return head->next;
- }
- bool check_head(waitqueue_node<T_Mutex> &node)
- {
- return head == &node;
- }
-
- bool is_empty()
- {
- return head == nullptr;
- }
-
- void queue(waitqueue_node<T_Mutex> *node)
- {
- node->next = nullptr;
- if (tail) {
- tail->next = node;
- }
- else {
- head = node;
- }
- tail = node;
- }
- };
-
-
- class loop_access {
- public:
- template <typename Loop>
- static typename Loop::mutex_t &get_base_lock(Loop &loop) noexcept
- {
- return loop.get_base_lock();
- }
- template <typename Loop>
- static rearm process_fd_rearm(Loop &loop, typename Loop::base_fd_watcher *bfw,
- rearm rearm_type) noexcept
- {
- return loop.process_fd_rearm(bfw, rearm_type);
- }
- template <typename Loop>
- static rearm process_primary_rearm(Loop &loop, typename Loop::base_bidi_fd_watcher *bdfw,
- rearm rearm_type) noexcept
- {
- return loop.process_primary_rearm(bdfw, rearm_type);
- }
- template <typename Loop>
- static rearm process_secondary_rearm(Loop &loop, typename Loop::base_bidi_fd_watcher * bdfw,
- base_watcher * outw, rearm rearm_type) noexcept
- {
- return loop.process_secondary_rearm(bdfw, outw, rearm_type);
- }
- template <typename Loop>
- static void process_signal_rearm(Loop &loop, typename Loop::base_signal_watcher * bsw,
- rearm rearm_type) noexcept
- {
- loop.process_signal_rearm(bsw, rearm_type);
- }
- template <typename Loop>
- static void process_child_watch_rearm(Loop &loop, typename Loop::base_child_watcher *bcw,
- rearm rearm_type) noexcept
- {
- loop.process_child_watch_rearm(bcw, rearm_type);
- }
- template <typename Loop>
- static void process_timer_rearm(Loop &loop, typename Loop::base_timer_watcher *btw,
- rearm rearm_type) noexcept
- {
- loop.process_timer_rearm(btw, rearm_type);
- }
- template <typename Loop>
- static void requeue_watcher(Loop &loop, base_watcher *watcher) noexcept
- {
- loop.requeue_watcher(watcher);
- }
- template <typename Loop>
- static void release_watcher(Loop &loop, base_watcher *watcher) noexcept
- {
- loop.release_watcher(watcher);
- }
- };
-
-
-
- template <typename Loop> void post_dispatch(Loop &loop, base_watcher *watcher, rearm rearm_type)
- {
- if (rearm_type == rearm::REMOVE) {
- loop_access::get_base_lock(loop).unlock();
- loop_access::release_watcher(loop, watcher);
- watcher->watch_removed();
- loop_access::get_base_lock(loop).lock();
- }
- else if (rearm_type == rearm::REQUEUE) {
- loop_access::requeue_watcher(loop, watcher);
- }
- }
-
- template <typename Loop> void post_dispatch(Loop &loop, bidi_fd_watcher<Loop> *bdfd_watcher,
- base_watcher *out_watcher, rearm rearm_type)
- {
- base_watcher *watcher = (base_watcher *)bdfd_watcher;
- if (rearm_type == rearm::REMOVE) {
- loop_access::get_base_lock(loop).unlock();
- loop_access::release_watcher(loop, watcher);
- loop_access::release_watcher(loop, out_watcher);
- watcher->watch_removed();
- loop_access::get_base_lock(loop).lock();
- }
- else if (rearm_type == rearm::REQUEUE) {
- loop_access::requeue_watcher(loop, watcher);
- }
- }
-
-
-
-
-
-
-
-
-
-
- template <typename Traits, typename LoopTraits> class event_dispatch
- {
- friend class dasynq::event_loop<typename LoopTraits::mutex_t, LoopTraits>;
- public:
- using mutex_t = typename LoopTraits::mutex_t;
- using traits_t = Traits;
- using delayed_init = dasynq::delayed_init;
- private:
-
- prio_queue event_queue;
-
- using base_signal_watcher = dprivate::base_signal_watcher<typename traits_t::sigdata_t>;
- using base_child_watcher = dprivate::base_child_watcher<typename traits_t::proc_status_t>;
- using base_timer_watcher = dprivate::base_timer_watcher;
-
-
-
- void prepare_watcher(base_watcher *bwatcher)
- {
- allocate_handle(event_queue, bwatcher->heap_handle, bwatcher);
- }
-
- void queue_watcher(base_watcher *bwatcher) noexcept
- {
- event_queue.insert(bwatcher->heap_handle, bwatcher->priority);
- }
-
- void dequeue_watcher(base_watcher *bwatcher) noexcept
- {
- if (event_queue.is_queued(bwatcher->heap_handle)) {
- event_queue.remove(bwatcher->heap_handle);
- }
- }
-
- void release_watcher(base_watcher *bwatcher) noexcept
- {
- event_queue.deallocate(bwatcher->heap_handle);
- }
-
- protected:
- mutex_t lock;
- template <typename T> void init(T *loop) noexcept { }
- void cleanup() noexcept { }
-
- void sigmaskf(int how, const sigset_t *set, sigset_t *oset)
- {
- LoopTraits::sigmaskf(how, set, oset);
- }
-
-
- template <typename T>
- bool receive_signal(T &loop_mech, typename Traits::sigdata_t & siginfo, void * userdata) noexcept
- {
- base_signal_watcher * bwatcher = static_cast<base_signal_watcher *>(userdata);
- bwatcher->siginfo = siginfo;
- queue_watcher(bwatcher);
- return true;
- }
-
-
-
-
- template <typename T>
- std::tuple<int, typename Traits::fd_s> receive_fd_event(T &loop_mech, typename Traits::fd_r fd_r,
- void * userdata, int flags) noexcept
- {
- base_fd_watcher * bfdw = static_cast<base_fd_watcher *>(userdata);
-
- bfdw->event_flags |= flags;
- typename Traits::fd_s watch_fd_s {bfdw->watch_fd};
-
- base_watcher * bwatcher = bfdw;
-
- bool is_multi_watch = bfdw->watch_flags & multi_watch;
- if (is_multi_watch) {
- base_bidi_fd_watcher *bbdw = static_cast<base_bidi_fd_watcher *>(bwatcher);
- bbdw->watch_flags &= ~flags;
- if ((flags & IN_EVENTS) && (flags & OUT_EVENTS)) {
-
- queue_watcher(&bbdw->out_watcher);
- }
- else if (flags & OUT_EVENTS) {
-
- bwatcher = &(bbdw->out_watcher);
- }
- }
- queue_watcher(bwatcher);
-
- if (is_multi_watch && ! traits_t::has_separate_rw_fd_watches) {
-
-
-
- int in_out_mask = IN_EVENTS | OUT_EVENTS;
- if ((bfdw->watch_flags & in_out_mask) != 0) {
-
- return std::make_tuple((bfdw->watch_flags & in_out_mask) | ONE_SHOT, watch_fd_s);
-
-
- }
- }
- return std::make_tuple(0, watch_fd_s);
- }
-
-
- void receive_child_stat(pid_t child, typename LoopTraits::backend_traits_t::proc_status_t status, void * userdata) noexcept
- {
- base_child_watcher * watcher = static_cast<base_child_watcher *>(userdata);
- watcher->child_status = status;
- watcher->child_termd = true;
- queue_watcher(watcher);
- }
-
- void receive_timer_expiry(timer_handle_t & timer_handle, void * userdata, int intervals) noexcept
- {
- base_timer_watcher * watcher = static_cast<base_timer_watcher *>(userdata);
- watcher->intervals += intervals;
- queue_watcher(watcher);
- }
-
-
-
- base_watcher * pull_queued_event() noexcept
- {
- if (event_queue.empty()) {
- return nullptr;
- }
-
- auto & rhndl = event_queue.get_root();
- base_watcher *r = dprivate::get_watcher(event_queue, rhndl);
- event_queue.pull_root();
- return r;
- }
-
- size_t num_queued_events() noexcept
- {
- return event_queue.size();
- }
-
-
- void issue_delete(base_watcher *watcher) noexcept
- {
-
-
-
-
- lock.lock();
-
- if (watcher->active) {
-
-
- watcher->deleteme = true;
- lock.unlock();
- }
- else {
-
- dequeue_watcher(watcher);
- release_watcher(watcher);
-
- lock.unlock();
- watcher->watch_removed();
- }
- }
-
-
-
- void issue_delete(base_bidi_fd_watcher *watcher) noexcept
- {
- lock.lock();
-
- if (watcher->active) {
- watcher->deleteme = true;
- release_watcher(watcher);
- }
- else {
- dequeue_watcher(watcher);
- release_watcher(watcher);
- watcher->read_removed = true;
- }
-
- base_watcher *secondary = &(watcher->out_watcher);
- if (secondary->active) {
- secondary->deleteme = true;
- release_watcher(watcher);
- }
- else {
- dequeue_watcher(secondary);
- release_watcher(watcher);
- watcher->write_removed = true;
- }
-
- if (watcher->read_removed && watcher->write_removed) {
- lock.unlock();
- watcher->watch_removed();
- }
- else {
- lock.unlock();
- }
- }
- event_dispatch() { }
- event_dispatch(const event_dispatch &) = delete;
- };
- }
- template <typename T_Mutex, typename Traits>
- class event_loop
- {
- using my_event_loop_t = event_loop<T_Mutex, Traits>;
- friend class dprivate::fd_watcher<my_event_loop_t>;
- friend class dprivate::bidi_fd_watcher<my_event_loop_t>;
- friend class dprivate::signal_watcher<my_event_loop_t>;
- friend class dprivate::child_proc_watcher<my_event_loop_t>;
- friend class dprivate::timer<my_event_loop_t>;
-
- friend class dprivate::loop_access;
- using backend_traits_t = typename Traits::backend_traits_t;
- template <typename T> using event_dispatch = dprivate::event_dispatch<T,Traits>;
- using dispatch_t = event_dispatch<backend_traits_t>;
- using loop_mech_t = typename Traits::template backend_t<dispatch_t>;
- using reaper_mutex_t = typename loop_mech_t::reaper_mutex_t;
- public:
- using traits_t = Traits;
- using loop_traits_t = typename loop_mech_t::traits_t;
- using mutex_t = T_Mutex;
-
- private:
- template <typename T> using waitqueue = dprivate::waitqueue<T>;
- template <typename T> using waitqueue_node = dprivate::waitqueue_node<T>;
- using base_watcher = dprivate::base_watcher;
- using base_signal_watcher = dprivate::base_signal_watcher<typename loop_traits_t::sigdata_t>;
- using base_fd_watcher = dprivate::base_fd_watcher;
- using base_bidi_fd_watcher = dprivate::base_bidi_fd_watcher;
- using base_child_watcher = dprivate::base_child_watcher<typename loop_traits_t::proc_status_t>;
- using base_timer_watcher = dprivate::base_timer_watcher;
- using watch_type_t = dprivate::watch_type_t;
- loop_mech_t loop_mech;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- mutex_t wait_lock;
- bool long_poll_running = false;
- waitqueue<mutex_t> attn_waitqueue;
- waitqueue<mutex_t> wait_waitqueue;
-
- mutex_t &get_base_lock() noexcept
- {
- return loop_mech.lock;
- }
-
- reaper_mutex_t &get_reaper_lock() noexcept
- {
- return loop_mech.get_reaper_lock();
- }
- void register_signal(base_signal_watcher *callBack, int signo)
- {
- std::lock_guard<mutex_t> guard(loop_mech.lock);
- loop_mech.prepare_watcher(callBack);
- try {
- loop_mech.add_signal_watch_nolock(signo, callBack);
- if (backend_traits_t::interrupt_after_signal_add) {
- interrupt_if_necessary();
- }
- }
- catch (...) {
- loop_mech.release_watcher(callBack);
- throw;
- }
- }
-
- void deregister(base_signal_watcher *callBack, int signo) noexcept
- {
- loop_mech.remove_signal_watch(signo);
-
- waitqueue_node<T_Mutex> qnode;
- get_attn_lock(qnode);
-
- loop_mech.issue_delete(callBack);
-
- release_lock(qnode);
- }
- void register_fd(base_fd_watcher *callback, int fd, int eventmask, bool enabled, bool emulate = false)
- {
- std::lock_guard<mutex_t> guard(loop_mech.lock);
- loop_mech.prepare_watcher(callback);
- try {
- if (! loop_mech.add_fd_watch(fd, callback, eventmask | ONE_SHOT, enabled, emulate)) {
- callback->emulatefd = true;
- callback->emulate_enabled = enabled;
- if (enabled) {
- callback->event_flags = eventmask & IO_EVENTS;
- if (eventmask & IO_EVENTS) {
- requeue_watcher(callback);
- }
- }
- }
- else if (enabled && backend_traits_t::interrupt_after_fd_add) {
- interrupt_if_necessary();
- }
- }
- catch (...) {
- loop_mech.release_watcher(callback);
- throw;
- }
- }
-
-
-
- void register_fd(base_bidi_fd_watcher *callback, int fd, int eventmask, bool emulate = false)
- {
- std::lock_guard<mutex_t> guard(loop_mech.lock);
- loop_mech.prepare_watcher(callback);
- try {
- loop_mech.prepare_watcher(&callback->out_watcher);
- try {
- bool do_interrupt = false;
- if (backend_traits_t::has_separate_rw_fd_watches) {
- int r = loop_mech.add_bidi_fd_watch(fd, callback, eventmask | ONE_SHOT, emulate);
- if (r & IN_EVENTS) {
- callback->emulatefd = true;
- if (eventmask & IN_EVENTS) {
- callback->watch_flags &= ~IN_EVENTS;
- requeue_watcher(callback);
- }
- }
- else if ((eventmask & IN_EVENTS) && backend_traits_t::interrupt_after_fd_add) {
- do_interrupt = true;
- }
- if (r & OUT_EVENTS) {
- callback->out_watcher.emulatefd = true;
- if (eventmask & OUT_EVENTS) {
- callback->watch_flags &= ~OUT_EVENTS;
- requeue_watcher(&callback->out_watcher);
- }
- }
- else if ((eventmask & OUT_EVENTS) && backend_traits_t::interrupt_after_fd_add) {
- do_interrupt = true;
- }
- }
- else {
- if (! loop_mech.add_fd_watch(fd, callback, eventmask | ONE_SHOT, true, emulate)) {
- callback->emulatefd = true;
- callback->out_watcher.emulatefd = true;
- if (eventmask & IN_EVENTS) {
- callback->watch_flags &= ~IN_EVENTS;
- requeue_watcher(callback);
- }
- if (eventmask & OUT_EVENTS) {
- callback->watch_flags &= ~OUT_EVENTS;
- requeue_watcher(&callback->out_watcher);
- }
- }
- else if (backend_traits_t::interrupt_after_fd_add) {
- do_interrupt = true;
- }
- }
- if (do_interrupt) {
- interrupt_if_necessary();
- }
- }
- catch (...) {
- loop_mech.release_watcher(&callback->out_watcher);
- throw;
- }
- }
- catch (...) {
- loop_mech.release_watcher(callback);
- throw;
- }
- }
-
- void set_fd_enabled(base_watcher *watcher, int fd, int watch_flags, bool enabled) noexcept
- {
- if (enabled) {
- loop_mech.enable_fd_watch(fd, watcher, watch_flags | ONE_SHOT);
- if (backend_traits_t::interrupt_after_fd_add) {
- interrupt_if_necessary();
- }
- }
- else {
- loop_mech.disable_fd_watch(fd, watch_flags);
- }
- }
- void set_fd_enabled_nolock(base_watcher *watcher, int fd, int watch_flags, bool enabled) noexcept
- {
- if (enabled) {
- loop_mech.enable_fd_watch_nolock(fd, watcher, watch_flags | ONE_SHOT);
- if (backend_traits_t::interrupt_after_fd_add) {
- interrupt_if_necessary();
- }
- }
- else {
- loop_mech.disable_fd_watch_nolock(fd, watch_flags);
- }
- }
-
- void deregister(base_fd_watcher *callback, int fd) noexcept
- {
- if (callback->emulatefd) {
- auto & ed = (dispatch_t &) loop_mech;
- ed.issue_delete(callback);
- return;
- }
-
- loop_mech.remove_fd_watch(fd, callback->watch_flags);
- waitqueue_node<T_Mutex> qnode;
- get_attn_lock(qnode);
-
- auto & ed = (dispatch_t &) loop_mech;
- ed.issue_delete(callback);
-
- release_lock(qnode);
- }
-
- void deregister(base_bidi_fd_watcher *callback, int fd) noexcept
- {
- if (backend_traits_t::has_separate_rw_fd_watches) {
- loop_mech.remove_bidi_fd_watch(fd);
- }
- else {
- loop_mech.remove_fd_watch(fd, callback->watch_flags);
- }
-
- waitqueue_node<T_Mutex> qnode;
- get_attn_lock(qnode);
-
- dispatch_t & ed = (dispatch_t &) loop_mech;
- ed.issue_delete(callback);
-
- release_lock(qnode);
- }
-
- void reserve_child_watch(base_child_watcher *callback)
- {
- std::lock_guard<mutex_t> guard(loop_mech.lock);
- loop_mech.prepare_watcher(callback);
- try {
- loop_mech.reserve_child_watch_nolock(callback->watch_handle);
- }
- catch (...) {
- loop_mech.release_watcher(callback);
- throw;
- }
- }
-
- void unreserve(base_child_watcher *callback) noexcept
- {
- std::lock_guard<mutex_t> guard(loop_mech.lock);
- loop_mech.unreserve_child_watch(callback->watch_handle);
- loop_mech.release_watcher(callback);
- }
-
- void register_child(base_child_watcher *callback, pid_t child)
- {
- std::lock_guard<mutex_t> guard(loop_mech.lock);
-
- loop_mech.prepare_watcher(callback);
- try {
- loop_mech.add_child_watch_nolock(callback->watch_handle, child, callback);
- }
- catch (...) {
- loop_mech.release_watcher(callback);
- throw;
- }
- }
-
- void register_reserved_child(base_child_watcher *callback, pid_t child) noexcept
- {
- loop_mech.add_reserved_child_watch(callback->watch_handle, child, callback);
- }
- void register_reserved_child_nolock(base_child_watcher *callback, pid_t child) noexcept
- {
- loop_mech.add_reserved_child_watch_nolock(callback->watch_handle, child, callback);
- }
-
- void deregister(base_child_watcher *callback, pid_t child) noexcept
- {
- loop_mech.remove_child_watch(callback->watch_handle);
- waitqueue_node<T_Mutex> qnode;
- get_attn_lock(qnode);
-
- loop_mech.issue_delete(callback);
-
- release_lock(qnode);
- }
-
-
-
- void stop_watch(base_child_watcher *callback) noexcept
- {
- loop_mech.stop_child_watch(callback->watch_handle);
- }
- void register_timer(base_timer_watcher *callback, clock_type clock)
- {
- std::lock_guard<mutex_t> guard(loop_mech.lock);
-
- loop_mech.prepare_watcher(callback);
- try {
- loop_mech.add_timer_nolock(callback->timer_handle, callback, clock);
- }
- catch (...) {
- loop_mech.release_watcher(callback);
- }
- }
-
- void set_timer(base_timer_watcher *callback, const timespec &timeout, clock_type clock) noexcept
- {
- struct timespec interval {0, 0};
- loop_mech.set_timer(callback->timer_handle, timeout, interval, true, clock);
- }
-
- void set_timer(base_timer_watcher *callback, const timespec &timeout, const timespec &interval,
- clock_type clock) noexcept
- {
- loop_mech.set_timer(callback->timer_handle, timeout, interval, true, clock);
- }
- void set_timer_rel(base_timer_watcher *callback, const timespec &timeout, clock_type clock) noexcept
- {
- struct timespec interval {0, 0};
- loop_mech.set_timer_rel(callback->timer_handle, timeout, interval, true, clock);
- }
-
- void set_timer_rel(base_timer_watcher *callback, const timespec &timeout,
- const timespec &interval, clock_type clock) noexcept
- {
- loop_mech.set_timer_rel(callback->timer_handle, timeout, interval, true, clock);
- }
- void set_timer_enabled(base_timer_watcher *callback, clock_type clock, bool enabled) noexcept
- {
- loop_mech.enable_timer(callback->timer_handle, enabled, clock);
- }
- void set_timer_enabled_nolock(base_timer_watcher *callback, clock_type clock, bool enabled) noexcept
- {
- loop_mech.enable_timer_nolock(callback->timer_handle, enabled, clock);
- }
- void stop_timer(base_timer_watcher *callback, clock_type clock) noexcept
- {
- loop_mech.stop_timer(callback->timer_handle, clock);
- }
- void deregister(base_timer_watcher *callback, clock_type clock) noexcept
- {
- loop_mech.remove_timer(callback->timer_handle, clock);
-
- waitqueue_node<T_Mutex> qnode;
- get_attn_lock(qnode);
-
- loop_mech.issue_delete(callback);
-
- release_lock(qnode);
- }
-
- void dequeue_watcher(base_watcher *watcher) noexcept
- {
- loop_mech.dequeue_watcher(watcher);
- }
- void requeue_watcher(base_watcher *watcher) noexcept
- {
- loop_mech.queue_watcher(watcher);
- interrupt_if_necessary();
- }
- void release_watcher(base_watcher *watcher) noexcept
- {
- loop_mech.release_watcher(watcher);
- }
-
-
- void interrupt_if_necessary()
- {
- wait_lock.lock();
- bool attn_q_empty = attn_waitqueue.is_empty();
- wait_lock.unlock();
- if (! attn_q_empty) {
- loop_mech.interrupt_wait();
- }
- }
-
-
-
-
- void get_attn_lock(waitqueue_node<T_Mutex> &qnode) noexcept
- {
- std::unique_lock<T_Mutex> ulock(wait_lock);
- attn_waitqueue.queue(&qnode);
- if (! attn_waitqueue.check_head(qnode)) {
- if (long_poll_running) {
-
-
-
-
- if (attn_waitqueue.get_second() == &qnode) {
- loop_mech.interrupt_wait();
- }
- }
- while (! attn_waitqueue.check_head(qnode)) {
- qnode.wait(ulock);
- }
- }
- }
-
-
-
- bool poll_attn_lock(waitqueue_node<T_Mutex> &qnode) noexcept
- {
- std::unique_lock<T_Mutex> ulock(wait_lock);
- if (long_poll_running) {
-
- return false;
- }
-
-
- attn_waitqueue.queue(&qnode);
- while (! attn_waitqueue.check_head(qnode)) {
- qnode.wait(ulock);
- }
- return true;
- }
-
-
-
-
- void get_pollwait_lock(waitqueue_node<T_Mutex> &qnode) noexcept
- {
- std::unique_lock<T_Mutex> ulock(wait_lock);
- if (attn_waitqueue.is_empty()) {
-
- attn_waitqueue.queue(&qnode);
- }
- else {
- wait_waitqueue.queue(&qnode);
- }
-
- while (! attn_waitqueue.check_head(qnode)) {
- qnode.wait(ulock);
- }
- long_poll_running = true;
- }
-
-
- void release_lock(waitqueue_node<T_Mutex> &qnode) noexcept
- {
- std::unique_lock<T_Mutex> ulock(wait_lock);
- long_poll_running = false;
- waitqueue_node<T_Mutex> * nhead = attn_waitqueue.unqueue();
- if (nhead != nullptr) {
-
- nhead->signal();
- }
- else {
-
-
- if (! wait_waitqueue.is_empty()) {
- auto nhead = wait_waitqueue.get_head();
- wait_waitqueue.unqueue();
- attn_waitqueue.queue(nhead);
- long_poll_running = true;
- nhead->signal();
- }
- }
- }
-
- void process_signal_rearm(base_signal_watcher * bsw, rearm rearm_type) noexcept
- {
-
- if (rearm_type == rearm::REARM) {
- loop_mech.rearm_signal_watch_nolock(bsw->siginfo.get_signo(), bsw);
- if (backend_traits_t::interrupt_after_signal_add) {
- interrupt_if_necessary();
- }
- }
- else if (rearm_type == rearm::REMOVE) {
- loop_mech.remove_signal_watch_nolock(bsw->siginfo.get_signo());
- }
-
- }
-
-
- rearm process_fd_rearm(base_fd_watcher * bfw, rearm rearm_type) noexcept
- {
- bool emulatedfd = static_cast<base_watcher *>(bfw)->emulatefd;
- if (emulatedfd) {
- if (rearm_type == rearm::REARM) {
- bfw->emulate_enabled = true;
- rearm_type = rearm::REQUEUE;
- }
- else if (rearm_type == rearm::DISARM) {
- bfw->emulate_enabled = false;
- }
- else if (rearm_type == rearm::NOOP) {
- if (bfw->emulate_enabled) {
- rearm_type = rearm::REQUEUE;
- }
- }
- }
- else if (rearm_type == rearm::REARM) {
- set_fd_enabled_nolock(bfw, bfw->watch_fd,
- bfw->watch_flags & (IN_EVENTS | OUT_EVENTS), true);
- }
- else if (rearm_type == rearm::DISARM) {
- loop_mech.disable_fd_watch_nolock(bfw->watch_fd, bfw->watch_flags);
- }
- else if (rearm_type == rearm::REMOVE) {
- loop_mech.remove_fd_watch_nolock(bfw->watch_fd, bfw->watch_flags);
- }
- return rearm_type;
- }
-
- rearm process_primary_rearm(base_bidi_fd_watcher * bdfw, rearm rearm_type) noexcept
- {
- bool emulatedfd = static_cast<base_watcher *>(bdfw)->emulatefd;
-
- if (rearm_type == rearm::REMOVE) {
- bdfw->read_removed = 1;
- if (backend_traits_t::has_separate_rw_fd_watches) {
- bdfw->watch_flags &= ~IN_EVENTS;
- if (! emulatedfd) {
- loop_mech.remove_fd_watch_nolock(bdfw->watch_fd, IN_EVENTS);
- }
- return bdfw->write_removed ? rearm::REMOVE : rearm::NOOP;
- }
- else {
- if (! bdfw->write_removed) {
- if (bdfw->watch_flags & IN_EVENTS) {
- bdfw->watch_flags &= ~IN_EVENTS;
- if (! emulatedfd) {
- set_fd_enabled_nolock(bdfw, bdfw->watch_fd, bdfw->watch_flags,
- bdfw->watch_flags != 0);
- }
- }
- return rearm::NOOP;
- }
- else {
-
- if (! emulatedfd) {
- loop_mech.remove_fd_watch_nolock(bdfw->watch_fd, 0 );
- }
- return rearm::REMOVE;
- }
- }
- }
- else if (rearm_type == rearm::DISARM) {
- bdfw->watch_flags &= ~IN_EVENTS;
- if (! emulatedfd) {
- if (! backend_traits_t::has_separate_rw_fd_watches) {
- int watch_flags = bdfw->watch_flags & (IN_EVENTS | OUT_EVENTS);
- set_fd_enabled_nolock(bdfw, bdfw->watch_fd, watch_flags, watch_flags != 0);
- }
- else {
- loop_mech.disable_fd_watch_nolock(bdfw->watch_fd, IN_EVENTS);
- }
- }
- }
- else if (rearm_type == rearm::REARM) {
- if (! emulatedfd) {
- bdfw->watch_flags |= IN_EVENTS;
- if (! backend_traits_t::has_separate_rw_fd_watches) {
- int watch_flags = bdfw->watch_flags;
- set_fd_enabled_nolock(bdfw, bdfw->watch_fd,
- watch_flags & (IN_EVENTS | OUT_EVENTS), true);
- }
- else {
- set_fd_enabled_nolock(bdfw, bdfw->watch_fd, IN_EVENTS, true);
- }
- }
- else {
- bdfw->watch_flags &= ~IN_EVENTS;
- rearm_type = rearm::REQUEUE;
- }
- }
- else if (rearm_type == rearm::NOOP) {
- if (bdfw->emulatefd) {
- if (bdfw->watch_flags & IN_EVENTS) {
- bdfw->watch_flags &= ~IN_EVENTS;
- rearm_type = rearm::REQUEUE;
- }
- }
- }
- return rearm_type;
- }
-
- rearm process_secondary_rearm(base_bidi_fd_watcher * bdfw, base_watcher * outw, rearm rearm_type) noexcept
- {
- bool emulatedfd = outw->emulatefd;
-
- if (emulatedfd) {
- if (rearm_type == rearm::REMOVE) {
- bdfw->write_removed = 1;
- bdfw->watch_flags &= ~OUT_EVENTS;
- rearm_type = bdfw->read_removed ? rearm::REMOVE : rearm::NOOP;
- }
- else if (rearm_type == rearm::DISARM) {
- bdfw->watch_flags &= ~OUT_EVENTS;
- }
- else if (rearm_type == rearm::REARM) {
- bdfw->watch_flags &= ~OUT_EVENTS;
- rearm_type = rearm::REQUEUE;
- }
- else if (rearm_type == rearm::NOOP) {
- if (bdfw->watch_flags & OUT_EVENTS) {
- bdfw->watch_flags &= ~OUT_EVENTS;
- rearm_type = rearm::REQUEUE;
- }
- }
- return rearm_type;
- }
- else if (rearm_type == rearm::REMOVE) {
- bdfw->write_removed = 1;
- if (backend_traits_t::has_separate_rw_fd_watches) {
- bdfw->watch_flags &= ~OUT_EVENTS;
- loop_mech.remove_fd_watch_nolock(bdfw->watch_fd, OUT_EVENTS);
- return bdfw->read_removed ? rearm::REMOVE : rearm::NOOP;
- }
- else {
- if (! bdfw->read_removed) {
- if (bdfw->watch_flags & OUT_EVENTS) {
- bdfw->watch_flags &= ~OUT_EVENTS;
- set_fd_enabled_nolock(bdfw, bdfw->watch_fd, bdfw->watch_flags, true);
- }
- return rearm::NOOP;
- }
- else {
-
- loop_mech.remove_fd_watch_nolock(bdfw->watch_fd, 0 );
- return rearm::REMOVE;
- }
- }
- }
- else if (rearm_type == rearm::DISARM) {
- bdfw->watch_flags &= ~OUT_EVENTS;
- if (! backend_traits_t::has_separate_rw_fd_watches) {
- int watch_flags = bdfw->watch_flags;
- set_fd_enabled_nolock(bdfw, bdfw->watch_fd, watch_flags & (IN_EVENTS | OUT_EVENTS), true);
- }
- else {
- loop_mech.disable_fd_watch_nolock(bdfw->watch_fd, OUT_EVENTS);
- }
- }
- else if (rearm_type == rearm::REARM) {
- bdfw->watch_flags |= OUT_EVENTS;
-
- if (! backend_traits_t::has_separate_rw_fd_watches) {
- int watch_flags = bdfw->watch_flags;
- set_fd_enabled_nolock(bdfw, bdfw->watch_fd, watch_flags & (IN_EVENTS | OUT_EVENTS), true);
- }
- else {
- set_fd_enabled_nolock(bdfw, bdfw->watch_fd, OUT_EVENTS | ONE_SHOT, true);
- }
- }
- return rearm_type;
- }
-
- void process_child_watch_rearm(base_child_watcher *bcw, rearm rearm_type) noexcept
- {
- if (rearm_type == rearm::REMOVE || rearm_type == rearm::DISARM) {
- loop_mech.unreserve_child_watch_nolock(bcw->watch_handle);
- }
- }
- void process_timer_rearm(base_timer_watcher *btw, rearm rearm_type) noexcept
- {
-
- if (rearm_type == rearm::REARM) {
- loop_mech.enable_timer_nolock(btw->timer_handle, true, btw->clock);
- }
- else if (rearm_type == rearm::REMOVE) {
- loop_mech.remove_timer_nolock(btw->timer_handle, btw->clock);
- }
- else if (rearm_type == rearm::DISARM) {
- loop_mech.enable_timer_nolock(btw->timer_handle, false, btw->clock);
- }
- }
-
-
-
- bool process_events(int limit) noexcept
- {
- loop_mech.lock.lock();
-
- if (limit == 0) {
- return false;
- }
-
-
-
-
-
-
-
- limit = std::min(size_t(limit), loop_mech.num_queued_events());
- base_watcher * pqueue = loop_mech.pull_queued_event();
- bool active = false;
-
- while (pqueue != nullptr) {
-
- pqueue->active = true;
- active = true;
-
- base_bidi_fd_watcher *bbfw = nullptr;
-
-
-
- if (pqueue->watchType == watch_type_t::SECONDARYFD) {
-
-
- uintptr_t rp = (uintptr_t)pqueue;
-
-
-
- _Pragma ("GCC diagnostic push")
- _Pragma ("GCC diagnostic ignored \"-Winvalid-offsetof\"")
- rp -= offsetof(base_bidi_fd_watcher, out_watcher);
- _Pragma ("GCC diagnostic pop")
- bbfw = (base_bidi_fd_watcher *)rp;
-
- bbfw->dispatch_second(this);
- }
- else {
- pqueue->dispatch(this);
- }
- if (limit > 0) {
- limit--;
- if (limit == 0) break;
- }
- pqueue = loop_mech.pull_queued_event();
- }
-
- loop_mech.lock.unlock();
- return active;
- }
- public:
-
- using fd_watcher = dprivate::fd_watcher<my_event_loop_t>;
- using bidi_fd_watcher = dprivate::bidi_fd_watcher<my_event_loop_t>;
- using signal_watcher = dprivate::signal_watcher<my_event_loop_t>;
- using child_proc_watcher = dprivate::child_proc_watcher<my_event_loop_t>;
- using timer = dprivate::timer<my_event_loop_t>;
-
- template <typename D> using fd_watcher_impl = dprivate::fd_watcher_impl<my_event_loop_t, D>;
- template <typename D> using bidi_fd_watcher_impl = dprivate::bidi_fd_watcher_impl<my_event_loop_t, D>;
- template <typename D> using signal_watcher_impl = dprivate::signal_watcher_impl<my_event_loop_t, D>;
- template <typename D> using child_proc_watcher_impl = dprivate::child_proc_watcher_impl<my_event_loop_t, D>;
- template <typename D> using timer_impl = dprivate::timer_impl<my_event_loop_t, D>;
-
-
- void run(int limit = -1) noexcept
- {
-
- waitqueue_node<T_Mutex> qnode;
- get_pollwait_lock(qnode);
- loop_mech.pull_events(false);
- release_lock(qnode);
- while (! process_events(limit)) {
-
- get_pollwait_lock(qnode);
- loop_mech.pull_events(true);
- release_lock(qnode);
- }
- }
-
- void poll(int limit = -1) noexcept
- {
- waitqueue_node<T_Mutex> qnode;
- if (poll_attn_lock(qnode)) {
- loop_mech.pull_events(false);
- release_lock(qnode);
- }
- process_events(limit);
- }
-
-
-
-
-
-
- void get_time(timespec &ts, clock_type clock, bool force_update = false) noexcept
- {
- loop_mech.get_time(ts, clock, force_update);
- }
- void get_time(time_val &tv, clock_type clock, bool force_update = false) noexcept
- {
- loop_mech.get_time(tv, clock, force_update);
- }
- event_loop() { }
- event_loop(delayed_init d) noexcept : loop_mech(d) { }
- event_loop(const event_loop &other) = delete;
-
- void init()
- {
- loop_mech.init();
- }
- };
- typedef event_loop<null_mutex> event_loop_n;
- typedef event_loop<std::mutex> event_loop_th;
- namespace dprivate {
- template <typename EventLoop>
- class signal_watcher : private dprivate::base_signal_watcher<typename EventLoop::loop_traits_t::sigdata_t>
- {
- template <typename, typename> friend class signal_watcher_impl;
- using base_watcher = dprivate::base_watcher;
- using T_Mutex = typename EventLoop::mutex_t;
-
- public:
- using event_loop_t = EventLoop;
- using siginfo_p = typename signal_watcher::siginfo_p;
-
-
-
-
- inline void add_watch(event_loop_t &eloop, int signo, int prio = DEFAULT_PRIORITY)
- {
- base_watcher::init();
- this->priority = prio;
- this->siginfo.set_signo(signo);
- eloop.register_signal(this, signo);
- }
-
- inline void deregister(event_loop_t &eloop) noexcept
- {
- eloop.deregister(this, this->siginfo.get_signo());
- }
-
- template <typename T>
- static signal_watcher<event_loop_t> *add_watch(event_loop_t &eloop, int signo, T watch_hndlr)
- {
- class lambda_sig_watcher : public signal_watcher_impl<event_loop_t, lambda_sig_watcher>
- {
- private:
- T watch_hndlr;
- public:
- lambda_sig_watcher(T watch_handlr_a) : watch_hndlr(watch_handlr_a)
- {
-
- }
- rearm received(event_loop_t &eloop, int signo, siginfo_p siginfo)
- {
- return watch_hndlr(eloop, signo, siginfo);
- }
- void watch_removed() noexcept override
- {
- delete this;
- }
- };
- lambda_sig_watcher * lsw = new lambda_sig_watcher(watch_hndlr);
- lsw->add_watch(eloop, signo);
- return lsw;
- }
-
- };
- template <typename EventLoop, typename Derived>
- class signal_watcher_impl : public signal_watcher<EventLoop>
- {
- void dispatch(void *loop_ptr) noexcept override
- {
- EventLoop &loop = *static_cast<EventLoop *>(loop_ptr);
- loop_access::get_base_lock(loop).unlock();
- auto rearm_type = static_cast<Derived *>(this)->received(loop, this->siginfo.get_signo(), this->siginfo);
- loop_access::get_base_lock(loop).lock();
- if (rearm_type != rearm::REMOVED) {
- this->active = false;
- if (this->deleteme) {
-
- rearm_type = rearm::REMOVE;
- }
- loop_access::process_signal_rearm(loop, this, rearm_type);
- post_dispatch(loop, this, rearm_type);
- }
- }
- };
- template <typename EventLoop>
- class fd_watcher : private dprivate::base_fd_watcher
- {
- template <typename, typename> friend class fd_watcher_impl;
- using base_watcher = dprivate::base_watcher;
- using mutex_t = typename EventLoop::mutex_t;
- protected:
-
-
-
-
-
- void set_watch_flags(int newFlags)
- {
- this->watch_flags = newFlags;
- }
-
- public:
-
- using event_loop_t = EventLoop;
-
-
-
-
-
-
-
-
-
-
-
-
-
- void add_watch(event_loop_t &eloop, int fd, int flags, bool enabled = true, int prio = DEFAULT_PRIORITY)
- {
- base_watcher::init();
- this->priority = prio;
- this->watch_fd = fd;
- this->watch_flags = flags;
- eloop.register_fd(this, fd, flags, enabled, true);
- }
- void add_watch_noemu(event_loop_t &eloop, int fd, int flags, bool enabled = true, int prio = DEFAULT_PRIORITY)
- {
- base_watcher::init();
- this->priority = prio;
- this->watch_fd = fd;
- this->watch_flags = flags;
- eloop.register_fd(this, fd, flags, enabled, false);
- }
-
- int get_watched_fd()
- {
- return this->watch_fd;
- }
-
-
-
-
-
-
-
-
-
- void deregister(event_loop_t &eloop) noexcept
- {
- eloop.deregister(this, this->watch_fd);
- }
-
- void set_enabled(event_loop_t &eloop, bool enable) noexcept
- {
- std::lock_guard<mutex_t> guard(eloop.get_base_lock());
- if (this->emulatefd) {
- if (enable && ! this->emulate_enabled) {
- loop_access::requeue_watcher(eloop, this);
- }
- this->emulate_enabled = enable;
- }
- else {
- eloop.set_fd_enabled_nolock(this, this->watch_fd, this->watch_flags, enable);
- }
- if (! enable) {
- eloop.dequeue_watcher(this);
- }
- }
-
-
-
- template <typename T>
- static fd_watcher<EventLoop> *add_watch(event_loop_t &eloop, int fd, int flags, T watchHndlr)
- {
- class lambda_fd_watcher : public fd_watcher_impl<event_loop_t, lambda_fd_watcher>
- {
- private:
- T watchHndlr;
- public:
- lambda_fd_watcher(T watchHandlr_a) : watchHndlr(watchHandlr_a)
- {
-
- }
- rearm fd_event(event_loop_t &eloop, int fd, int flags)
- {
- return watchHndlr(eloop, fd, flags);
- }
- void watch_removed() noexcept override
- {
- delete this;
- }
- };
-
- lambda_fd_watcher * lfd = new lambda_fd_watcher(watchHndlr);
- lfd->add_watch(eloop, fd, flags);
- return lfd;
- }
-
-
- };
- template <typename EventLoop, typename Derived>
- class fd_watcher_impl : public fd_watcher<EventLoop>
- {
- void dispatch(void *loop_ptr) noexcept override
- {
- EventLoop &loop = *static_cast<EventLoop *>(loop_ptr);
-
- this->emulate_enabled = false;
- loop_access::get_base_lock(loop).unlock();
- auto rearm_type = static_cast<Derived *>(this)->fd_event(loop, this->watch_fd, this->event_flags);
- loop_access::get_base_lock(loop).lock();
- if (rearm_type != rearm::REMOVED) {
- this->event_flags = 0;
- this->active = false;
- if (this->deleteme) {
-
- rearm_type = rearm::REMOVE;
- }
- rearm_type = loop_access::process_fd_rearm(loop, this, rearm_type);
- post_dispatch(loop, this, rearm_type);
- }
- }
- };
- template <typename EventLoop>
- class bidi_fd_watcher : private dprivate::base_bidi_fd_watcher
- {
- template <typename, typename> friend class bidi_fd_watcher_impl;
- using base_watcher = dprivate::base_watcher;
- using mutex_t = typename EventLoop::mutex_t;
-
- void set_watch_enabled(EventLoop &eloop, bool in, bool b)
- {
- int events = in ? IN_EVENTS : OUT_EVENTS;
- auto orig_flags = this->watch_flags;
-
- if (b) {
- this->watch_flags |= events;
- }
- else {
- this->watch_flags &= ~events;
- }
- dprivate::base_watcher * watcher = in ? this : &this->out_watcher;
- if (! watcher->emulatefd) {
- if (EventLoop::loop_traits_t::has_separate_rw_fd_watches) {
- eloop.set_fd_enabled_nolock(this, this->watch_fd, events | ONE_SHOT, b);
- }
- else {
- eloop.set_fd_enabled_nolock(this, this->watch_fd,
- (this->watch_flags & IO_EVENTS) | ONE_SHOT,
- (this->watch_flags & IO_EVENTS) != 0);
- }
- }
- else {
-
- if (b && (orig_flags != this->watch_flags)) {
- this->watch_flags = orig_flags;
- loop_access::requeue_watcher(eloop, watcher);
- }
- }
- if (! b) {
- eloop.dequeue_watcher(watcher);
- }
- }
-
- public:
- using event_loop_t = EventLoop;
- void set_in_watch_enabled(event_loop_t &eloop, bool b) noexcept
- {
- eloop.get_base_lock().lock();
- set_watch_enabled(eloop, true, b);
- eloop.get_base_lock().unlock();
- }
-
- void set_out_watch_enabled(event_loop_t &eloop, bool b) noexcept
- {
- eloop.get_base_lock().lock();
- set_watch_enabled(eloop, false, b);
- eloop.get_base_lock().unlock();
- }
-
-
-
-
-
-
-
-
-
- void set_watches(event_loop_t &eloop, int new_flags) noexcept
- {
- std::lock_guard<mutex_t> guard(eloop.get_base_lock());
- bool use_emulation = this->emulatefd || this->out_watcher.emulatefd;
- if (use_emulation || EventLoop::loop_traits_t::has_separate_rw_fd_watches) {
- set_watch_enabled(eloop, true, (new_flags & IN_EVENTS) != 0);
- set_watch_enabled(eloop, false, (new_flags & OUT_EVENTS) != 0);
- }
- else {
- this->watch_flags = (this->watch_flags & ~IO_EVENTS) | new_flags;
- eloop.set_fd_enabled_nolock((dprivate::base_watcher *) this, this->watch_fd, this->watch_flags & IO_EVENTS, true);
- }
- }
-
-
-
-
-
- void add_watch(event_loop_t &eloop, int fd, int flags, int inprio = DEFAULT_PRIORITY, int outprio = DEFAULT_PRIORITY)
- {
- base_watcher::init();
- this->out_watcher.base_watcher::init();
- this->watch_fd = fd;
- this->watch_flags = flags | dprivate::multi_watch;
- this->read_removed = false;
- this->write_removed = false;
- this->priority = inprio;
- this->set_priority(this->out_watcher, outprio);
- eloop.register_fd(this, fd, flags, true);
- }
- void add_watch_noemu(event_loop_t &eloop, int fd, int flags, int inprio = DEFAULT_PRIORITY, int outprio = DEFAULT_PRIORITY)
- {
- base_watcher::init();
- this->out_watcher.base_watcher::init();
- this->watch_fd = fd;
- this->watch_flags = flags | dprivate::multi_watch;
- this->read_removed = false;
- this->write_removed = false;
- this->priority = inprio;
- this->set_priority(this->out_watcher, outprio);
- eloop.register_fd(this, fd, flags, false);
- }
- int get_watched_fd()
- {
- return this->watch_fd;
- }
-
-
-
-
-
-
-
-
-
- void deregister(event_loop_t &eloop) noexcept
- {
- eloop.deregister(this, this->watch_fd);
- }
-
- template <typename T>
- static bidi_fd_watcher<event_loop_t> *add_watch(event_loop_t &eloop, int fd, int flags, T watch_hndlr)
- {
- class lambda_bidi_watcher : public bidi_fd_watcher_impl<event_loop_t, lambda_bidi_watcher>
- {
- private:
- T watch_hndlr;
- public:
- lambda_bidi_watcher(T watch_handlr_a) : watch_hndlr(watch_handlr_a)
- {
-
- }
- rearm read_ready(event_loop_t &eloop, int fd)
- {
- return watch_hndlr(eloop, fd, IN_EVENTS);
- }
- rearm write_ready(event_loop_t &eloop, int fd)
- {
- return watch_hndlr(eloop, fd, OUT_EVENTS);
- }
- void watch_removed() noexcept override
- {
- delete this;
- }
- };
- lambda_bidi_watcher * lfd = new lambda_bidi_watcher(watch_hndlr);
- lfd->add_watch(eloop, fd, flags);
- return lfd;
- }
-
-
- };
- template <typename EventLoop, typename Derived>
- class bidi_fd_watcher_impl : public bidi_fd_watcher<EventLoop>
- {
- void dispatch(void *loop_ptr) noexcept override
- {
- EventLoop &loop = *static_cast<EventLoop *>(loop_ptr);
- this->emulate_enabled = false;
- loop_access::get_base_lock(loop).unlock();
- auto rearm_type = static_cast<Derived *>(this)->read_ready(loop, this->watch_fd);
- loop_access::get_base_lock(loop).lock();
- if (rearm_type != rearm::REMOVED) {
- this->event_flags &= ~IN_EVENTS;
- this->active = false;
- if (this->deleteme) {
-
- rearm_type = rearm::REMOVE;
- }
- rearm_type = loop_access::process_primary_rearm(loop, this, rearm_type);
- auto &outwatcher = bidi_fd_watcher<EventLoop>::out_watcher;
- post_dispatch(loop, this, &outwatcher, rearm_type);
- }
- }
- void dispatch_second(void *loop_ptr) noexcept override
- {
- auto &outwatcher = bidi_fd_watcher<EventLoop>::out_watcher;
- EventLoop &loop = *static_cast<EventLoop *>(loop_ptr);
- loop_access::get_base_lock(loop).unlock();
- auto rearm_type = static_cast<Derived *>(this)->write_ready(loop, this->watch_fd);
- loop_access::get_base_lock(loop).lock();
- if (rearm_type != rearm::REMOVED) {
- this->event_flags &= ~OUT_EVENTS;
- outwatcher.active = false;
- if (outwatcher.deleteme) {
-
- rearm_type = rearm::REMOVE;
- }
- rearm_type = loop_access::process_secondary_rearm(loop, this, &outwatcher, rearm_type);
- if (rearm_type == rearm::REQUEUE) {
- post_dispatch(loop, &outwatcher, rearm_type);
- }
- else {
- post_dispatch(loop, this, &outwatcher, rearm_type);
- }
- }
- }
- };
- template <typename EventLoop>
- class child_proc_watcher : private dprivate::base_child_watcher<typename EventLoop::loop_traits_t::proc_status_t>
- {
- template <typename, typename> friend class child_proc_watcher_impl;
- using base_watcher = dprivate::base_watcher;
- using mutex_t = typename EventLoop::mutex_t;
- public:
- using event_loop_t = EventLoop;
- using proc_status_t = typename EventLoop::loop_traits_t::proc_status_t;
-
-
-
- int send_signal(event_loop_t &loop, int signo) noexcept
- {
- auto reaper_mutex = loop.get_reaper_lock();
- std::lock_guard<decltype(reaper_mutex)> guard(reaper_mutex);
- if (this->child_termd) {
- errno = ESRCH;
- return -1;
- }
- return kill(this->watch_pid, signo);
- }
-
-
-
- void reserve_watch(event_loop_t &eloop)
- {
- eloop.reserve_child_watch(this);
- }
-
- void unreserve(event_loop_t &eloop)
- {
- eloop.unreserve(this);
- }
-
-
-
-
-
- void add_watch(event_loop_t &eloop, pid_t child, int prio = DEFAULT_PRIORITY)
- {
- base_watcher::init();
- this->watch_pid = child;
- this->priority = prio;
- eloop.register_child(this, child);
- }
-
-
-
-
-
-
-
- void add_reserved(event_loop_t &eloop, pid_t child, int prio = DEFAULT_PRIORITY) noexcept
- {
- base_watcher::init();
- this->watch_pid = child;
- this->priority = prio;
- eloop.register_reserved_child(this, child);
- }
-
- void deregister(event_loop_t &eloop, pid_t child) noexcept
- {
- eloop.deregister(this, child);
- }
-
-
- void stop_watch(event_loop_t &eloop) noexcept
- {
- eloop.stop_watch(this);
- }
-
-
-
-
-
-
-
- pid_t fork(event_loop_t &eloop, bool from_reserved = false, int prio = DEFAULT_PRIORITY)
- {
- base_watcher::init();
- this->priority = prio;
- if (EventLoop::loop_traits_t::supports_childwatch_reservation) {
-
- if (! from_reserved) {
- reserve_watch(eloop);
- }
-
- auto &lock = eloop.get_base_lock();
- lock.lock();
-
- pid_t child = ::fork();
- if (child == -1) {
-
- lock.unlock();
- unreserve(eloop);
- throw std::system_error(errno, std::system_category());
- }
-
- if (child == 0) {
-
- lock.unlock();
- return 0;
- }
-
-
- this->watch_pid = child;
- eloop.register_reserved_child_nolock(this, child);
- lock.unlock();
- return child;
- }
- else {
- int pipefds[2];
- if (pipe2(pipefds, O_CLOEXEC) == -1) {
- throw std::system_error(errno, std::system_category());
- }
-
- std::lock_guard<mutex_t> guard(eloop.get_base_lock());
-
- pid_t child = ::fork();
- if (child == -1) {
- throw std::system_error(errno, std::system_category());
- }
-
- if (child == 0) {
-
- close(pipefds[1]);
-
-
- int rr;
- int r = read(pipefds[0], &rr, sizeof(rr));
- while (r == -1 && errno == EINTR) {
- r = read(pipefds[0], &rr, sizeof(rr));
- }
-
- if (r <= 0) _exit(0);
-
- close(pipefds[0]);
- return 0;
- }
-
- close(pipefds[0]);
-
-
- try {
- this->watch_pid = child;
- eloop.register_child(this, child);
-
-
- write(pipefds[1], &pipefds, sizeof(int));
- close(pipefds[1]);
-
- return child;
- }
- catch (...) {
- close(pipefds[1]);
- throw;
- }
- }
- }
-
-
- };
- template <typename EventLoop, typename Derived>
- class child_proc_watcher_impl : public child_proc_watcher<EventLoop>
- {
- void dispatch(void *loop_ptr) noexcept override
- {
- EventLoop &loop = *static_cast<EventLoop *>(loop_ptr);
- loop_access::get_base_lock(loop).unlock();
- auto rearm_type = static_cast<Derived *>(this)->status_change(loop, this->watch_pid, this->child_status);
- loop_access::get_base_lock(loop).lock();
- if (rearm_type != rearm::REMOVED) {
- this->active = false;
- if (this->deleteme) {
-
- rearm_type = rearm::REMOVE;
- }
- loop_access::process_child_watch_rearm(loop, this, rearm_type);
-
- post_dispatch(loop, this, rearm_type);
- }
- }
- };
- template <typename EventLoop>
- class timer : private base_timer_watcher
- {
- template <typename, typename> friend class timer_impl;
- using base_t = base_timer_watcher;
- using mutex_t = typename EventLoop::mutex_t;
- public:
- using event_loop_t = EventLoop;
-
- void add_timer(event_loop_t &eloop, clock_type clock = clock_type::MONOTONIC, int prio = DEFAULT_PRIORITY)
- {
- base_watcher::init();
- this->priority = prio;
- this->clock = clock;
- this->intervals = 0;
- eloop.register_timer(this, clock);
- }
-
- void arm_timer(event_loop_t &eloop, const timespec &timeout) noexcept
- {
- eloop.set_timer(this, timeout, base_t::clock);
- }
-
- void arm_timer(event_loop_t &eloop, const timespec &timeout, const timespec &interval) noexcept
- {
- eloop.set_timer(this, timeout, interval, base_t::clock);
- }
-
- void arm_timer_rel(event_loop_t &eloop, const timespec &timeout) noexcept
- {
- eloop.set_timer_rel(this, timeout, base_t::clock);
- }
-
- void arm_timer_rel(event_loop_t &eloop, const timespec &timeout,
- const timespec &interval) noexcept
- {
- eloop.set_timer_rel(this, timeout, interval, base_t::clock);
- }
-
- void stop_timer(event_loop_t &eloop) noexcept
- {
- eloop.stop_timer(this, base_t::clock);
- }
- void set_enabled(event_loop_t &eloop, clock_type clock, bool enabled) noexcept
- {
- std::lock_guard<mutex_t> guard(eloop.get_base_lock());
- eloop.set_timer_enabled_nolock(this, clock, enabled);
- if (! enabled) {
- eloop.dequeue_watcher(this);
- }
- }
- void deregister(event_loop_t &eloop) noexcept
- {
- eloop.deregister(this, this->clock);
- }
- template <typename T>
- static timer<EventLoop> *add_timer(EventLoop &eloop, clock_type clock, bool relative,
- const timespec &timeout, const timespec &interval, T watch_hndlr)
- {
- class lambda_timer : public timer_impl<event_loop_t, lambda_timer>
- {
- private:
- T watch_hndlr;
- public:
- lambda_timer(T watch_handlr_a) : watch_hndlr(watch_handlr_a)
- {
-
- }
- rearm timer_expiry(event_loop_t &eloop, int intervals)
- {
- return watch_hndlr(eloop, intervals);
- }
- void watch_removed() noexcept override
- {
- delete this;
- }
- };
- lambda_timer * lt = new lambda_timer(watch_hndlr);
- lt->add_timer(eloop, clock);
- if (relative) {
- lt->arm_timer_rel(eloop, timeout, interval);
- }
- else {
- lt->arm_timer(eloop, timeout, interval);
- }
- return lt;
- }
-
-
-
-
- };
- template <typename EventLoop, typename Derived>
- class timer_impl : public timer<EventLoop>
- {
- void dispatch(void *loop_ptr) noexcept override
- {
- EventLoop &loop = *static_cast<EventLoop *>(loop_ptr);
- loop_access::get_base_lock(loop).unlock();
- auto intervals_report = this->intervals;
- this->intervals = 0;
- auto rearm_type = static_cast<Derived *>(this)->timer_expiry(loop, intervals_report);
- loop_access::get_base_lock(loop).lock();
- if (rearm_type != rearm::REMOVED) {
- this->active = false;
- if (this->deleteme) {
-
- rearm_type = rearm::REMOVE;
- }
- loop_access::process_timer_rearm(loop, this, rearm_type);
- post_dispatch(loop, this, rearm_type);
- }
- }
- };
- }
- }
- }
- #endif
|