1*fbb2e0a3Schristos
2*fbb2e0a3Schristos.. _threadpool:
3*fbb2e0a3Schristos
4*fbb2e0a3SchristosThread pool work scheduling
5*fbb2e0a3Schristos===========================
6*fbb2e0a3Schristos
7*fbb2e0a3Schristoslibuv provides a threadpool which can be used to run user code and get notified
8*fbb2e0a3Schristosin the loop thread. This thread pool is internally used to run all file system
9*fbb2e0a3Schristosoperations, as well as getaddrinfo and getnameinfo requests.
10*fbb2e0a3Schristos
11*fbb2e0a3SchristosIts default size is 4, but it can be changed at startup time by setting the
12*fbb2e0a3Schristos``UV_THREADPOOL_SIZE`` environment variable to any value (the absolute maximum
13*fbb2e0a3Schristosis 1024).
14*fbb2e0a3Schristos
15*fbb2e0a3Schristos.. versionchanged:: 1.30.0 the maximum UV_THREADPOOL_SIZE allowed was increased from 128 to 1024.
16*fbb2e0a3Schristos
17*fbb2e0a3SchristosThe threadpool is global and shared across all event loops. When a particular
18*fbb2e0a3Schristosfunction makes use of the threadpool (i.e. when using :c:func:`uv_queue_work`)
19*fbb2e0a3Schristoslibuv preallocates and initializes the maximum number of threads allowed by
20*fbb2e0a3Schristos``UV_THREADPOOL_SIZE``. This causes a relatively minor memory overhead
21*fbb2e0a3Schristos(~1MB for 128 threads) but increases the performance of threading at runtime.
22*fbb2e0a3Schristos
23*fbb2e0a3Schristos.. note::
24*fbb2e0a3Schristos    Note that even though a global thread pool which is shared across all events
25*fbb2e0a3Schristos    loops is used, the functions are not thread safe.
26*fbb2e0a3Schristos
27*fbb2e0a3Schristos
28*fbb2e0a3SchristosData types
29*fbb2e0a3Schristos----------
30*fbb2e0a3Schristos
31*fbb2e0a3Schristos.. c:type:: uv_work_t
32*fbb2e0a3Schristos
33*fbb2e0a3Schristos    Work request type.
34*fbb2e0a3Schristos
35*fbb2e0a3Schristos.. c:type:: void (*uv_work_cb)(uv_work_t* req)
36*fbb2e0a3Schristos
37*fbb2e0a3Schristos    Callback passed to :c:func:`uv_queue_work` which will be run on the thread
38*fbb2e0a3Schristos    pool.
39*fbb2e0a3Schristos
40*fbb2e0a3Schristos.. c:type:: void (*uv_after_work_cb)(uv_work_t* req, int status)
41*fbb2e0a3Schristos
42*fbb2e0a3Schristos    Callback passed to :c:func:`uv_queue_work` which will be called on the loop
43*fbb2e0a3Schristos    thread after the work on the threadpool has been completed. If the work
44*fbb2e0a3Schristos    was cancelled using :c:func:`uv_cancel` `status` will be ``UV_ECANCELED``.
45*fbb2e0a3Schristos
46*fbb2e0a3Schristos
47*fbb2e0a3SchristosPublic members
48*fbb2e0a3Schristos^^^^^^^^^^^^^^
49*fbb2e0a3Schristos
50*fbb2e0a3Schristos.. c:member:: uv_loop_t* uv_work_t.loop
51*fbb2e0a3Schristos
52*fbb2e0a3Schristos    Loop that started this request and where completion will be reported.
53*fbb2e0a3Schristos    Readonly.
54*fbb2e0a3Schristos
55*fbb2e0a3Schristos.. seealso:: The :c:type:`uv_req_t` members also apply.
56*fbb2e0a3Schristos
57*fbb2e0a3Schristos
58*fbb2e0a3SchristosAPI
59*fbb2e0a3Schristos---
60*fbb2e0a3Schristos
61*fbb2e0a3Schristos.. c:function:: int uv_queue_work(uv_loop_t* loop, uv_work_t* req, uv_work_cb work_cb, uv_after_work_cb after_work_cb)
62*fbb2e0a3Schristos
63*fbb2e0a3Schristos    Initializes a work request which will run the given `work_cb` in a thread
64*fbb2e0a3Schristos    from the threadpool. Once `work_cb` is completed, `after_work_cb` will be
65*fbb2e0a3Schristos    called on the loop thread.
66*fbb2e0a3Schristos
67*fbb2e0a3Schristos    This request can be cancelled with :c:func:`uv_cancel`.
68*fbb2e0a3Schristos
69*fbb2e0a3Schristos.. seealso:: The :c:type:`uv_req_t` API functions also apply.
70