| 1 | /* |
| 2 | Copyright(C) 2016 Brazil |
| 3 | Copyright(C) 2018-2022 Sutou Kouhei <[email protected]> |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include "../grn_ctx_impl.h" |
| 21 | |
| 22 | #ifdef GRN_WITH_MRUBY |
| 23 | #include <mruby.h> |
| 24 | |
| 25 | #include "mrb_thread.h" |
| 26 | |
| 27 | static mrb_value |
| 28 | thread_get_limit(mrb_state *mrb, mrb_value self) |
| 29 | { |
| 30 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
| 31 | uint32_t limit; |
| 32 | limit = grn_thread_get_limit_with_ctx(ctx); |
| 33 | return mrb_int_value(mrb, limit); |
| 34 | } |
| 35 | |
| 36 | static mrb_value |
| 37 | thread_set_limit(mrb_state *mrb, mrb_value self) |
| 38 | { |
| 39 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
| 40 | mrb_int limit; |
| 41 | |
| 42 | mrb_get_args(mrb, "i", &limit); |
| 43 | if (limit < 1) { |
| 44 | mrb_raisef(mrb, E_ARGUMENT_ERROR, |
| 45 | "thread limit must be 1 or larger: %S", |
| 46 | mrb_int_value(mrb, limit)); |
| 47 | } |
| 48 | grn_thread_set_limit_with_ctx(ctx, (uint32_t)limit); |
| 49 | return mrb_nil_value(); |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | grn_mrb_thread_init(grn_ctx *ctx) |
| 54 | { |
| 55 | mrb_state *mrb = ctx->impl->mrb.state; |
| 56 | struct RClass *module = ctx->impl->mrb.module; |
| 57 | struct RClass *thread_module; |
| 58 | |
| 59 | thread_module = mrb_define_module_under(mrb, module, "Thread"); |
| 60 | |
| 61 | mrb_define_class_method(mrb, thread_module, |
| 62 | "limit", thread_get_limit, MRB_ARGS_NONE()); |
| 63 | mrb_define_class_method(mrb, thread_module, |
| 64 | "limit=", thread_set_limit, MRB_ARGS_REQ(1)); |
| 65 | } |
| 66 | #endif |