diff options
Diffstat (limited to 'nuttx/mm/shm/shmdt.c')
-rw-r--r--[-rwxr-xr-x] | nuttx/mm/shm/shmdt.c | 148 |
1 files changed, 130 insertions, 18 deletions
diff --git a/nuttx/mm/shm/shmdt.c b/nuttx/mm/shm/shmdt.c index a350e5ca0..8acfc6197 100755..100644 --- a/nuttx/mm/shm/shmdt.c +++ b/nuttx/mm/shm/shmdt.c @@ -33,17 +33,23 @@ * ****************************************************************************/ -/****************************************************************************
- * Included Files
- ****************************************************************************/
-
-#include <nuttx/config.h>
-
-#include <sys/shm.h>
-#include <errno.h>
-
-#ifdef CONFIG_MM_SHM
-
+/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <sys/shm.h> +#include <errno.h> + +#include <nuttx/arch.h> +#include <nuttx/sched.h> +#include <nuttx/shm.h> + +#include "shm/shm.h" + +#ifdef CONFIG_MM_SHM + /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ @@ -87,13 +93,119 @@ * The value of shmaddr is not the data segment start address of a * shared memory segment. * - ****************************************************************************/
-
+ ****************************************************************************/ + int shmdt(FAR const void *shmaddr) -{
-#warning Not implemented
- set_errno(ENOSYS);
- return ERROR;
-}
+{ + FAR struct shm_region_s *region; + FAR struct task_group_s *group; + FAR struct tcb_s *tcb; + unsigned int npages; + int shmid; + int ret; + + /* Get the TCB and group containing our virtual memory allocator */ + + tcb = sched_self(); + DEBUGASSERT(tcb && tcb->group); + group = tcb->group; + DEBUGASSERT(group->tg_shm.gs_handle != NULL); + + /* Perform the reverse lookup to get the shmid corresponding to this + * shmaddr. + */ + + for (shmid = 0; + shmid < CONFIG_ARCH_SHM_MAXREGIONS && + group->tg_shm.gs_vaddr[shmid] != (uintptr_t)shmaddr; + shmid++); + + if (shmid >= CONFIG_ARCH_SHM_MAXREGIONS) + { + shmdbg("No region matching this virtual address: %p\n", shmaddr); + ret = -EINVAL; + goto errout_with_errno; + } + + /* Get the region associated with the shmid */ + + region = &g_shminfo.si_region[shmid]; + DEBUGASSERT((region->sr_flags & SRFLAG_INUSE) != 0); + + /* Get exclusive access to the region data structure */ + + ret = sem_wait(®ion->sr_sem); + if (ret < 0) + { + shmdbg("sem_wait failed: %d\n", ret); + goto errout; + } + + /* Free the virtual address space */ + + gran_free(group->tg_shm.gs_handle, (FAR void *)shmaddr, + region->sr_ds.shm_segsz); + + /* Convert the region size to pages */ + + npages = MM_PGALIGNUP(region->sr_ds.shm_segsz); + + /* Detach, i.e, unmap, on shared memory region from a user virtual + * address. + */ + + ret = up_shmdt((uintptr_t)shmaddr, npages); + if (ret < 0) + { + shmdbg("up_shmdt() failed\n"); + } + + /* Indicate that there is no longer any mapping for this region. */ + + group->tg_shm.gs_vaddr[shmid] = 0; + + /* Decrement the count of processes attached to this region. + * If the count decrements to zero and there is a pending unlink, + * then destroy the shared memory region now and stop any further + * operations on it. + */ + + DEBUGASSERT(region->sr_ds.shm_nattch > 0); + if (region->sr_ds.shm_nattch <= 1) + { + region->sr_ds.shm_nattch = 0; + if ((region->sr_flags & SRFLAG_UNLINKED) != 0) + { + shm_destroy(shmid); + return OK; + } + } + else + { + /* Just decrement the number of processes attached to the shared + * memory region. + */ + + region->sr_ds.shm_nattch--; + } + + /* Save the process ID of the the last operation */ + + region->sr_ds.shm_lpid = tcb->pid; + + /* Save the time of the last shmdt() */ + + region->sr_ds.shm_dtime = time(NULL); + + /* Release our lock on the entry */ + + sem_post(®ion->sr_sem); + return OK; + +errout_with_errno: + set_errno(-ret); +errout: + return ERROR; +} #endif /* CONFIG_MM_SHM */ |