gang.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * SPU file system
  3. *
  4. * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
  5. *
  6. * Author: Arnd Bergmann <arndb@de.ibm.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/list.h>
  23. #include <linux/slab.h>
  24. #include "spufs.h"
  25. struct spu_gang *alloc_spu_gang(void)
  26. {
  27. struct spu_gang *gang;
  28. gang = kzalloc(sizeof *gang, GFP_KERNEL);
  29. if (!gang)
  30. goto out;
  31. kref_init(&gang->kref);
  32. mutex_init(&gang->mutex);
  33. INIT_LIST_HEAD(&gang->list);
  34. out:
  35. return gang;
  36. }
  37. static void destroy_spu_gang(struct kref *kref)
  38. {
  39. struct spu_gang *gang;
  40. gang = container_of(kref, struct spu_gang, kref);
  41. WARN_ON(gang->contexts || !list_empty(&gang->list));
  42. kfree(gang);
  43. }
  44. struct spu_gang *get_spu_gang(struct spu_gang *gang)
  45. {
  46. kref_get(&gang->kref);
  47. return gang;
  48. }
  49. int put_spu_gang(struct spu_gang *gang)
  50. {
  51. return kref_put(&gang->kref, &destroy_spu_gang);
  52. }
  53. void spu_gang_add_ctx(struct spu_gang *gang, struct spu_context *ctx)
  54. {
  55. mutex_lock(&gang->mutex);
  56. ctx->gang = get_spu_gang(gang);
  57. list_add(&ctx->gang_list, &gang->list);
  58. gang->contexts++;
  59. mutex_unlock(&gang->mutex);
  60. }
  61. void spu_gang_remove_ctx(struct spu_gang *gang, struct spu_context *ctx)
  62. {
  63. mutex_lock(&gang->mutex);
  64. WARN_ON(ctx->gang != gang);
  65. list_del_init(&ctx->gang_list);
  66. gang->contexts--;
  67. mutex_unlock(&gang->mutex);
  68. put_spu_gang(gang);
  69. }