# HG changeset patch # User Olaf Wintermann # Date 1642845971 -3600 # Node ID 3dfbd0b919506324381a9cb744d9ebaab05a950b # Parent 65ac1342ba1f3b5dd1abfe4eb5ba45f3b7871c61 add ResourcePool initialization diff -r 65ac1342ba1f -r 3dfbd0b91950 src/server/config/serverconfig.c --- a/src/server/config/serverconfig.c Thu Jan 20 16:04:58 2022 +0100 +++ b/src/server/config/serverconfig.c Sat Jan 22 11:06:11 2022 +0100 @@ -193,7 +193,7 @@ //printf("%s [%.*s]\n", token_type_str(token.type), (int)token.content.length, token.content.ptr); switch(token.type) { - CFG_NO_TOKEN: break; + case CFG_NO_TOKEN: break; case CFG_TOKEN_COMMENT: { if(current->type == CONFIG_NODE_SPACE) { current->type = CONFIG_NODE_COMMENT; @@ -321,3 +321,20 @@ } return scstrn(NULL, 0); } + +sstr_t serverconfig_arg_name_value(UcxAllocator *a, scstr_t str, scstr_t *name) { + int valstart = 0; + for(int i=0;imax > RESOURCE_POOL_MAX_ALLOC) { + respool->max = RESOURCE_POOL_MAX_ALLOC; + log_ereport(LOG_WARN, "Resource pool %s: limit max to %d", name.ptr, respool->max); + } + + respool->resalloc = respool->max; + respool->resources = pool_malloc(cfg->pool, respool->resalloc * sizeof(void*)); + + if(!respool->resources || ucx_map_sstr_put(cfg->resources, name, respool)) { + log_ereport(LOG_FAILURE, "Cannot add resource pool: OOM"); + // the only cleanup we have to do + restype->destroy(respool_data); + return 1; + } + + pthread_mutex_init(&respool->lock, NULL); + pthread_cond_init(&respool->available, NULL); + + if(resourcepool_create_resources(respool, respool->max)) { + log_ereport(LOG_FAILURE, "Resource pool %s: Cannot create resources", name.ptr); + resourcepool_destroy(respool); + return 1; + } + + return 0; +} + +int resourcepool_create_resources(ResourcePool *pool, int num_res) { + if(num_res > pool->resalloc) { + num_res = pool->resalloc; + } + + for(int i=pool->numresources;itype->createresource(pool->data); + if(resource) { + pool->resources[pool->numresources++] = resource; + } else { + return 1; // error + } + } + + return 0; +} ResourceData* resourcepool_lookup(Session *sn, Request *rq, const char *name, int flags) { NSAPIRequest *request = (NSAPIRequest*)rq; @@ -47,12 +141,21 @@ // TODO: get cached resource + /* ResourceType *type = ucx_map_cstr_get(cfg->resources, name); if(!type) { return NULL; } + */ + + + return NULL; } void resourcepool_free(ResourceData *data) { } + +void resourcepool_destroy(ResourcePool *respool) { + // TODO +} diff -r 65ac1342ba1f -r 3dfbd0b91950 src/server/daemon/resourcepool.h --- a/src/server/daemon/resourcepool.h Thu Jan 20 16:04:58 2022 +0100 +++ b/src/server/daemon/resourcepool.h Sat Jan 22 11:06:11 2022 +0100 @@ -1,7 +1,7 @@ /* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * - * Copyright 2020 Olaf Wintermann. All rights reserved. + * Copyright 2021 Olaf Wintermann. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -31,6 +31,10 @@ #include "../public/nsapi.h" +#include "config.h" + +#include + #ifdef __cplusplus extern "C" { #endif @@ -45,14 +49,62 @@ }; struct ResourcePool { + /* + * Memory pool + */ + pool_handle_t *pool; + + /* + * Type information of this resource pool + * The type object is stored in the registered types map and should not + * be freed, when the resource pool is destroyed + */ ResourceType *type; + /* + * Data returned by the ResourceType init function + * When the pool is destroyed, the data should be passed to + * ResourceType.destroy + */ + void *data; + pthread_mutex_t lock; + pthread_cond_t available; + + /* + * Array of available resources + * each entry is created with ResourceType.createresource + */ + void **resources; + /* + * Allocated size of the resources array + */ + size_t resalloc; + + /* + * Number of currently available resources in the array + */ + size_t numresources; + + /* + * resource pool min parameter + */ int min; + + /* + * resource pool max parameter + */ int max; }; +int init_resource_pools(void); + +int resourcepool_new(ServerConfiguration *cfg, scstr_t type, scstr_t name, ConfigNode *node); + +int resourcepool_create_resources(ResourcePool *pool, int num_res); + +void resourcepool_destroy(ResourcePool *respool); #ifdef __cplusplus } diff -r 65ac1342ba1f -r 3dfbd0b91950 src/server/daemon/vfs.c --- a/src/server/daemon/vfs.c Thu Jan 20 16:04:58 2022 +0100 +++ b/src/server/daemon/vfs.c Sat Jan 22 11:06:11 2022 +0100 @@ -419,6 +419,9 @@ DIR *sys_dir = fdopendir(dir_fd); #endif if(!sys_dir) { + if(dir_fd > 0) { + close(dir_fd); + } if(ctx) { ctx->vfs_errno = errno; sys_set_error_status(ctx); diff -r 65ac1342ba1f -r 3dfbd0b91950 src/server/daemon/webserver.c --- a/src/server/daemon/webserver.c Thu Jan 20 16:04:58 2022 +0100 +++ b/src/server/daemon/webserver.c Sat Jan 22 11:06:11 2022 +0100 @@ -60,6 +60,7 @@ #include "log.h" #include "auth.h" #include "srvctrl.h" +#include "resourcepool.h" extern struct FuncStruct webserver_funcs[]; @@ -78,6 +79,11 @@ func_init(); add_functions(webserver_funcs); + // init resource pools + if(init_resource_pools()) { + return -1; + } + // load init.conf if(load_init_conf("config/init.conf")) { return -1; diff -r 65ac1342ba1f -r 3dfbd0b91950 src/server/public/nsapi.h --- a/src/server/public/nsapi.h Thu Jan 20 16:04:58 2022 +0100 +++ b/src/server/public/nsapi.h Sat Jan 22 11:06:11 2022 +0100 @@ -768,21 +768,23 @@ typedef struct _http_listener HttpListener; typedef struct ResourceType ResourceType; -typedef struct ResourceData ResourceData; +typedef struct ResourceData ResourceData; struct ResourceType { - ResourceType * (*init)(pool_handle_t *, pblock *); - void (*destroy)(ResourceType *); + void * (*init)(pool_handle_t *, pblock *); + void (*destroy)(void *); - void * (*createresource)(ResourceType *, pblock *); - void (*freeresource)(ResourceType *, void *); + void * (*createresource)(void *); + void (*freeresource)(void *, void *); + int (*prepare)(void *, void *); + int (*finish)(void *, void *); }; struct ResourceData { + void *resourcepool; void *data; }; - ////// /* * VSInitFunc, VSDestroyFunc, VSDirectiveInitFunc and VSDirectiveDestroyFunc