gang.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. mutex_init(&gang->aff_mutex);
  34. INIT_LIST_HEAD(&gang->list);
  35. INIT_LIST_HEAD(&gang->aff_list_head);
  36. out:
  37. return gang;
  38. }
  39. static void destroy_spu_gang(struct kref *kref)
  40. {
  41. struct spu_gang *gang;
  42. gang = container_of(kref, struct spu_gang, kref);
  43. WARN_ON(gang->contexts || !list_empty(&gang->list));
  44. kfree(gang);
  45. }
  46. struct spu_gang *get_spu_gang(struct spu_gang *gang)
  47. {
  48. kref_get(&gang->kref);
  49. return gang;
  50. }
  51. int put_spu_gang(struct spu_gang *gang)
  52. {
  53. return kref_put(&gang->kref, &destroy_spu_gang);
  54. }
  55. void spu_gang_add_ctx(struct spu_gang *gang, struct spu_context *ctx)
  56. {
  57. mutex_lock(&gang->mutex);
  58. ctx->gang = get_spu_gang(gang);
  59. list_add(&ctx->gang_list, &gang->list);
  60. gang->contexts++;
  61. mutex_unlock(&gang->mutex);
  62. }
  63. void spu_gang_remove_ctx(struct spu_gang *gang, struct spu_context *ctx)
  64. {
  65. mutex_lock(&gang->mutex);
  66. WARN_ON(ctx->gang != gang);
  67. if (!list_empty(&ctx->aff_list)) {
  68. list_del_init(&ctx->aff_list);
  69. gang->aff_flags &= ~AFF_OFFSETS_SET;
  70. }
  71. list_del_init(&ctx->gang_list);
  72. gang->contexts--;
  73. mutex_unlock(&gang->mutex);
  74. put_spu_gang(gang);
  75. }