… | |
… | |
890 | afterwards. |
890 | afterwards. |
891 | |
891 | |
892 | Ideally, C<release> will just call your mutex_unlock function, and |
892 | Ideally, C<release> will just call your mutex_unlock function, and |
893 | C<acquire> will just call the mutex_lock function again. |
893 | C<acquire> will just call the mutex_lock function again. |
894 | |
894 | |
|
|
895 | While event loop modifications are allowed between invocations of |
|
|
896 | C<release> and C<acquire> (that's their only purpose after all), no |
|
|
897 | modifications done will affect the event loop, i.e. adding watchers will |
|
|
898 | have no effect on the set of file descriptors being watched, or the time |
|
|
899 | waited. USe an C<ev_async> watcher to wake up C<ev_loop> when you want it |
|
|
900 | to take note of any changes you made. |
|
|
901 | |
|
|
902 | In theory, threads executing C<ev_loop> will be async-cancel safe between |
|
|
903 | invocations of C<release> and C<acquire>. |
|
|
904 | |
|
|
905 | See also the locking example in the C<THREADS> section later in this |
|
|
906 | document. |
|
|
907 | |
895 | =item ev_set_userdata (loop, void *data) |
908 | =item ev_set_userdata (loop, void *data) |
896 | |
909 | |
897 | =item ev_userdata (loop) |
910 | =item ev_userdata (loop) |
898 | |
911 | |
899 | Set and retrieve a single C<void *> associated with a loop. When |
912 | Set and retrieve a single C<void *> associated with a loop. When |
… | |
… | |
3928 | |
3941 | |
3929 | =back |
3942 | =back |
3930 | |
3943 | |
3931 | =head4 THREAD LOCKING EXAMPLE |
3944 | =head4 THREAD LOCKING EXAMPLE |
3932 | |
3945 | |
|
|
3946 | Here is a fictitious example of how to run an event loop in a different |
|
|
3947 | thread than where callbacks are being invoked and watchers are |
|
|
3948 | created/added/removed. |
|
|
3949 | |
|
|
3950 | For a real-world example, see the C<EV::Loop::Async> perl module, |
|
|
3951 | which uses exactly this technique (which is suited for many high-level |
|
|
3952 | languages). |
|
|
3953 | |
|
|
3954 | The example uses a pthread mutex to protect the loop data, a condition |
|
|
3955 | variable to wait for callback invocations, an async watcher to notify the |
|
|
3956 | event loop thread and an unspecified mechanism to wake up the main thread. |
|
|
3957 | |
|
|
3958 | First, you need to associate some data with the event loop: |
|
|
3959 | |
|
|
3960 | typedef struct { |
|
|
3961 | mutex_t lock; /* global loop lock */ |
|
|
3962 | ev_async async_w; |
|
|
3963 | thread_t tid; |
|
|
3964 | cond_t invoke_cv; |
|
|
3965 | } userdata; |
|
|
3966 | |
|
|
3967 | void prepare_loop (EV_P) |
|
|
3968 | { |
|
|
3969 | // for simplicity, we use a static userdata struct. |
|
|
3970 | static userdata u; |
|
|
3971 | |
|
|
3972 | ev_async_init (&u->async_w, async_cb); |
|
|
3973 | ev_async_start (EV_A_ &u->async_w); |
|
|
3974 | |
|
|
3975 | pthread_mutex_init (&u->lock, 0); |
|
|
3976 | pthread_cond_init (&u->invoke_cv, 0); |
|
|
3977 | |
|
|
3978 | // now associate this with the loop |
|
|
3979 | ev_set_userdata (EV_A_ u); |
|
|
3980 | ev_set_invoke_pending_cb (EV_A_ l_invoke); |
|
|
3981 | ev_set_loop_release_cb (EV_A_ l_release, l_acquire); |
|
|
3982 | |
|
|
3983 | // then create the thread running ev_loop |
|
|
3984 | pthread_create (&u->tid, 0, l_run, EV_A); |
|
|
3985 | } |
|
|
3986 | |
|
|
3987 | The callback for the C<ev_async> watcher does nothing: the watcher is used |
|
|
3988 | solely to wake up the event loop so it takes notice of any new watchers |
|
|
3989 | that might have been added: |
|
|
3990 | |
|
|
3991 | static void |
|
|
3992 | async_cb (EV_P_ ev_async *w, int revents) |
|
|
3993 | { |
|
|
3994 | // just used for the side effects |
|
|
3995 | } |
|
|
3996 | |
|
|
3997 | The C<l_release> and C<l_acquire> callbacks simply unlock/lock the mutex |
|
|
3998 | protecting the loop data, respectively. |
|
|
3999 | |
|
|
4000 | static void |
|
|
4001 | l_release (EV_P) |
|
|
4002 | { |
|
|
4003 | udat *u = ev_userdata (EV_A); |
|
|
4004 | pthread_mutex_unlock (&u->lock); |
|
|
4005 | } |
|
|
4006 | |
|
|
4007 | static void |
|
|
4008 | l_acquire (EV_P) |
|
|
4009 | { |
|
|
4010 | udat *u = ev_userdata (EV_A); |
|
|
4011 | pthread_mutex_lock (&u->lock); |
|
|
4012 | } |
|
|
4013 | |
|
|
4014 | The event loop thread first acquires the mutex, and then jumps straight |
|
|
4015 | into C<ev_loop>: |
|
|
4016 | |
|
|
4017 | void * |
|
|
4018 | l_run (void *thr_arg) |
|
|
4019 | { |
|
|
4020 | struct ev_loop *loop = (struct ev_loop *)thr_arg; |
|
|
4021 | |
|
|
4022 | l_acquire (EV_A); |
|
|
4023 | pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0); |
|
|
4024 | ev_loop (EV_A_ 0); |
|
|
4025 | l_release (EV_A); |
|
|
4026 | |
|
|
4027 | return 0; |
|
|
4028 | } |
|
|
4029 | |
|
|
4030 | Instead of invoking all pending watchers, the C<l_invoke> callback will |
|
|
4031 | signal the main thread via some unspecified mechanism (signals? pipe |
|
|
4032 | writes? C<Async::Interrupt>?) and then waits until all pending watchers |
|
|
4033 | have been called: |
|
|
4034 | |
|
|
4035 | static void |
|
|
4036 | l_invoke (EV_P) |
|
|
4037 | { |
|
|
4038 | udat *u = ev_userdata (EV_A); |
|
|
4039 | |
|
|
4040 | wake_up_other_thread_in_some_magic_or_not_so_magic_way (); |
|
|
4041 | |
|
|
4042 | pthread_cond_wait (&u->invoke_cv, &u->lock); |
|
|
4043 | } |
|
|
4044 | |
|
|
4045 | Now, whenever the main thread gets told to invoke pending watchers, it |
|
|
4046 | will grab the lock, call C<ev_invoke_pending> and then signal the loop |
|
|
4047 | thread to continue: |
|
|
4048 | |
|
|
4049 | static void |
|
|
4050 | real_invoke_pending (EV_P) |
|
|
4051 | { |
|
|
4052 | udat *u = ev_userdata (EV_A); |
|
|
4053 | |
|
|
4054 | pthread_mutex_lock (&u->lock); |
|
|
4055 | ev_invoke_pending (EV_A); |
|
|
4056 | pthread_cond_signal (&u->invoke_cv); |
|
|
4057 | pthread_mutex_unlock (&u->lock); |
|
|
4058 | } |
|
|
4059 | |
|
|
4060 | Whenever you want to start/stop a watcher or do other modifications to an |
|
|
4061 | event loop, you will now have to lock: |
|
|
4062 | |
|
|
4063 | ev_timer timeout_watcher; |
|
|
4064 | udat *u = ev_userdata (EV_A); |
|
|
4065 | |
|
|
4066 | ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.); |
|
|
4067 | |
|
|
4068 | pthread_mutex_lock (&u->lock); |
|
|
4069 | ev_timer_start (EV_A_ &timeout_watcher); |
|
|
4070 | ev_async_send (EV_A_ &u->async_w); |
|
|
4071 | pthread_mutex_unlock (&u->lock); |
|
|
4072 | |
|
|
4073 | Note that sending the C<ev_async> watcher is required because otherwise |
|
|
4074 | an event loop currently blocking in the kernel will have no knowledge |
|
|
4075 | about the newly added timer. By waking up the loop it will pick up any new |
|
|
4076 | watchers in the next event loop iteration. |
|
|
4077 | |
3933 | =head3 COROUTINES |
4078 | =head3 COROUTINES |
3934 | |
4079 | |
3935 | Libev is very accommodating to coroutines ("cooperative threads"): |
4080 | Libev is very accommodating to coroutines ("cooperative threads"): |
3936 | libev fully supports nesting calls to its functions from different |
4081 | libev fully supports nesting calls to its functions from different |
3937 | coroutines (e.g. you can call C<ev_loop> on the same loop from two |
4082 | coroutines (e.g. you can call C<ev_loop> on the same loop from two |